Skip to content

Instantly share code, notes, and snippets.

@binarymatt
Created March 25, 2012 01:19
Show Gist options
  • Save binarymatt/2190617 to your computer and use it in GitHub Desktop.
Save binarymatt/2190617 to your computer and use it in GitHub Desktop.
Redis-py namespace idea
from redis import Redis
class RedisNS(Redis):
def __init__(self, *args, **kwargs):
self.namespace = None
ns = kwargs.pop('namespace', None)
if ns:
self.namespace = '%s:' % ns
super(RedisNS, self).__init__(*args, **kwargs)
def _ns(self, key):
if self.namespace:
return self.namespace + ':' + key
return key
def debug_object(self, key):
"Returns version specific metainformation about a give key"
return super(RedisNS, self).debug_object(self._ns(key))
def delete(self, *names):
"Delete one or more keys specified by ``names``"
names = [self._ns(n) for n in names]
return super(RedisNS, self).delete(*names)
def object(self, infotype, key):
"Return the encoding, idletime, or refcount about the key"
return super(RedisNS, self).object(infotype, self._ns(key))
def append(self, key, value):
"""
Appends the string ``value`` to the value at ``key``. If ``key``
doesn't already exist, create it with a value of ``value``.
Returns the new length of the value at ``key``.
"""
return super(RedisNS, self).append(self._ns(key), value)
def decr(self, name, amount=1):
"""
Decrements the value of ``key`` by ``amount``. If no key exists,
the value will be initialized as 0 - ``amount``
"""
return super(RedisNS, self).decr(self._ns(name), amount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment