Navigation Menu

Skip to content

Commit

Permalink
Added support for OBJECT command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Reza Lotun committed Sep 21, 2011
1 parent 72facec commit 4d89fb2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
29 changes: 26 additions & 3 deletions txredis/protocol.py
Expand Up @@ -97,6 +97,8 @@ class InvalidResponse(RedisError):
class InvalidData(RedisError): class InvalidData(RedisError):
pass pass


class InvalidCommand(RedisError):
pass


class RedisBase(protocol.Protocol, policies.TimeoutMixin, object): class RedisBase(protocol.Protocol, policies.TimeoutMixin, object):
"""The main Redis client.""" """The main Redis client."""
Expand Down Expand Up @@ -553,9 +555,30 @@ def get_type(self, key):
Determine the type stored at key Determine the type stored at key
""" """
self._send('TYPE', key) self._send('TYPE', key)
res = self.getResponse() return self.getResponse()
# return None if res == 'none' else res
return res def get_object(self, key, refcount=False, encoding=False, idletime=False):
"""
Inspect the internals of Redis objects.
@param key : The Redis key you want to inspect
@param refcount : Returns the number of refereces of the value
associated with the specified key.
@param encoding : Returns the kind of internal representation for value.
@param idletime Returns the number of seconds since the object stored
at the specified key is idle. (Currently the actual
resolution is 10 seconds.)
"""
subcommand = ''
if idletime:
subcommand = 'IDLETIME'
elif encoding:
subcommand = 'ENCODING'
elif refcount:
subcommand = 'REFCOUNT'
if not subcommand:
raise InvalidCommand('Need a subcommand')
self._send('OBJECT', subcommand, key)
return self.getResponse()


# Commands operating on the key space # Commands operating on the key space
def keys(self, pattern): def keys(self, pattern):
Expand Down
15 changes: 15 additions & 0 deletions txredis/test/test_redis.py
Expand Up @@ -127,6 +127,21 @@ def test_delete(self):
ex = 2 ex = 2
t(a, ex) t(a, ex)


@defer.inlineCallbacks
def test_get_object(self):
r = self.redis
t = self.assertEqual
a = yield r.set('obj', 1)
ex = 'OK'
t(a, ex)

a = yield r.get_object('obj', idletime=True)
self.assertEqual(type(a), int)

a = yield r.get_object('obj', encoding=True)
ex = 'int'
t(a, ex)

@defer.inlineCallbacks @defer.inlineCallbacks
def test_get_type(self): def test_get_type(self):
r = self.redis r = self.redis
Expand Down

0 comments on commit 4d89fb2

Please sign in to comment.