Skip to content

Commit 6646ba7

Browse files
committed
implemented the OBJECT command. Fix for redis#137
1 parent 87f1315 commit 6646ba7

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

redis/client.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ def parse_debug_object(response):
6060

6161
return response
6262

63+
def parse_object(response, infotype):
64+
"Parse the results of an OBJECT command"
65+
if infotype in ('idletime', 'refcount'):
66+
return int(response)
67+
return response
68+
6369
def parse_info(response):
6470
"Parse the result of Redis's INFO command into a Python dict"
6571
info = {}
@@ -166,10 +172,11 @@ class StrictRedis(object):
166172
'BGSAVE': lambda r: r == 'Background saving started',
167173
'BRPOPLPUSH': lambda r: r and r or None,
168174
'CONFIG': parse_config,
175+
'DEBUG': parse_debug_object,
169176
'HGETALL': lambda r: r and pairs_to_dict(r) or {},
170177
'INFO': parse_info,
171-
'DEBUG': parse_debug_object,
172178
'LASTSAVE': timestamp_to_datetime,
179+
'OBJECT': parse_object,
173180
'PING': lambda r: r == 'PONG',
174181
'RANDOMKEY': lambda r: r and r or None,
175182
}
@@ -306,6 +313,10 @@ def dbsize(self):
306313
"Returns the number of keys in the current database"
307314
return self.execute_command('DBSIZE')
308315

316+
def debug_object(self, key):
317+
"Returns version specific metainformation about a give key"
318+
return self.execute_command('DEBUG', 'OBJECT', key)
319+
309320
def delete(self, *names):
310321
"Delete one or more keys specified by ``names``"
311322
return self.execute_command('DEL', *names)
@@ -327,17 +338,17 @@ def info(self):
327338
"Returns a dictionary containing information about the Redis server"
328339
return self.execute_command('INFO')
329340

330-
def debug_object(self, key):
331-
"Returns version specific metainformation about a give key"
332-
return self.execute_command('DEBUG', 'OBJECT', key)
333-
334341
def lastsave(self):
335342
"""
336343
Return a Python datetime object representing the last time the
337344
Redis database was saved to disk
338345
"""
339346
return self.execute_command('LASTSAVE')
340347

348+
def object(self, infotype, key):
349+
"Return the encoding, idletime, or refcount about the key"
350+
return self.execute_command('OBJECT', infotype, key, infotype=infotype)
351+
341352
def ping(self):
342353
"Ping the Redis server"
343354
return self.execute_command('PING')

tests/server_commands.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ def test_config_set(self):
8282
self.assert_(self.client.config_set('dbfilename', rdbname))
8383
self.assertEquals(self.client.config_get()['dbfilename'], rdbname)
8484

85+
def test_debug_object(self):
86+
self.client['a'] = 'foo'
87+
debug_info = self.client.debug_object('a')
88+
self.assert_(len(debug_info) > 0)
89+
self.assertEquals(debug_info['refcount'], 1)
90+
self.assert_(debug_info['serializedlength'] > 0)
91+
self.client.rpush('b', 'a1')
92+
debug_info = self.client.debug_object('a')
93+
8594
def test_echo(self):
8695
self.assertEquals(self.client.echo('foo bar'), 'foo bar')
8796

@@ -92,18 +101,15 @@ def test_info(self):
92101
self.assert_(isinstance(info, dict))
93102
self.assertEquals(info['db9']['keys'], 2)
94103

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

107+
def test_object(self):
108+
self.client['a'] = 'foo'
109+
self.assert_(isinstance(self.client.object('refcount', 'a'), int))
110+
self.assert_(isinstance(self.client.object('idletime', 'a'), int))
111+
self.assertEquals(self.client.object('encoding', 'a'), 'raw')
112+
107113
def test_ping(self):
108114
self.assertEquals(self.client.ping(), True)
109115

0 commit comments

Comments
 (0)