Skip to content

Commit 0904a88

Browse files
author
Shaneal Manek
committed
add a debug_object command - so we can get a key's size (among other things)
1 parent fb83cd0 commit 0904a88

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

redis/client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ def dict_merge(*dicts):
4444
[merged.update(d) for d in dicts]
4545
return merged
4646

47+
def parse_debug_object(response):
48+
"Parse the results of Redis's DEBUG OBJECT command into a Python dict"
49+
res = dict([kv.split(':') for kv in ('type:' + response).split()])
50+
51+
# parse some expected int values from the string response
52+
# note: this cmd isn't spec'd so these may not appear in all redis versions
53+
possible_int_fields = ['refcount', 'serializedlength',
54+
'lru', 'lru_seconds_idle']
55+
for field in possible_int_fields:
56+
if field in res:
57+
res[field] = int(res[field])
58+
59+
return res
60+
4761
def parse_info(response):
4862
"Parse the result of Redis's INFO command into a Python dict"
4963
info = {}
@@ -151,6 +165,7 @@ class StrictRedis(object):
151165
'CONFIG': parse_config,
152166
'HGETALL': lambda r: r and pairs_to_dict(r) or {},
153167
'INFO': parse_info,
168+
'DEBUG' : parse_debug_object,
154169
'LASTSAVE': timestamp_to_datetime,
155170
'PING': lambda r: r == 'PONG',
156171
'RANDOMKEY': lambda r: r and r or None,
@@ -309,6 +324,10 @@ def info(self):
309324
"Returns a dictionary containing information about the Redis server"
310325
return self.execute_command('INFO')
311326

327+
def debug_object(self, key):
328+
"""Returns version specific metainformation about a give key"""
329+
return self.execute_command('DEBUG', 'OBJECT', key)
330+
312331
def lastsave(self):
313332
"""
314333
Return a Python datetime object representing the last time the

tests/server_commands.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ def test_info(self):
9191
self.assert_(isinstance(info, dict))
9292
self.assertEquals(info['db9']['keys'], 2)
9393

94+
def test_debug_object(self):
95+
self.client['a'] = 'foo'
96+
debug_info = self.client.debug_object('a')
97+
self.assert_(len(debug_info) > 0)
98+
self.assertEquals(1, debug_info['refcount'])
99+
self.assert_(debug_info['serializedlength'] > 0)
100+
self.client.rpush('b', 'a1')
101+
debug_info = self.client.debug_object('a')
102+
94103
def test_lastsave(self):
95104
self.assert_(isinstance(self.client.lastsave(), datetime.datetime))
96105

0 commit comments

Comments
 (0)