Skip to content
Browse files

added Prefixed class, for prefixed proxies

  • Loading branch information...
1 parent 04b22ef commit 873eb16807fc1863d5d27486fa24495e89152da8 @egradman committed Oct 20, 2010
Showing with 37 additions and 0 deletions.
  1. +37 −0 redish/proxy.py
View
37 redish/proxy.py
@@ -126,4 +126,41 @@ def __delitem__(self, k):
def multikey(self, pattern):
for p in self.keys(pattern):
yield self[p]
+
+ def prefixed(self, prefix):
+ return Prefixed(self, prefix)
+
+class Prefixed(object):
+ """Decorates a Proxy object, such that any key passed to the underlying
+ proxy automatically has the prefix prepended
+ Note: this only works on proxy calls. it does not alter the underlying
+ behavior of Redis object methods. For example, the "keys()" method is
+ not prefixed
+
+ Example:
+ p = Proxy()
+ pp = Proxy.prefixed('foo:')
+ pp['bar'] = 1
+ pp.keys('foo:*')
+ -> ['foo:bar']
+ """
+
+ def __init__(self, proxy, prefix):
+ """record the underlying proxy, so we can reuse proxies"""
+ self.proxy = proxy
+ self.prefix = prefix
+ __getitem__ = lambda self, key: self.proxy.__getitem__(self.prefix+key)
+ __setitem__ = lambda self, key, value: self.proxy.__setitem__(self.prefix+key, value)
+ __contains__ = lambda self, key: self.proxy.__contains__(self.prefix+key)
+ __delitem = lambda self, key: self.proxy.__delitem__(self.prefix+key)
+
+ def multikey(self, pattern):
+ for p in self.proxy.keys(pattern):
+ yield self.proxy[self.prefix+p]
+
+ def __getattr__(self, attr):
+ """the prefixed object should behave like the proxy object, so delegate
+ any other attribute accesses to the underlying proxy
+ """
+ return getattr(self.proxy, attr)

0 comments on commit 873eb16

Please sign in to comment.
Something went wrong with that request. Please try again.