Commits

Jesper Nøhr committed 3f47d73

adding redis cache, fixing docstring in memcacher

  • Participants
  • Parent commits 415353b

Comments (0)

Files changed (2)

File heechee/webdav/caching/memcached.py

-
 import logging
 
 try:
     from md5 import md5
 
 class MemcachedCache(object):
-    
     """
-    Simple disk cache, using hashes of the keys as filenames.
-    Might well have undetected collisions; only for toy/test use.
-    """
-    
+    Simple memcache cache.
+    """    
     def __init__(self, servers, timeout=600):
         """
         Constructor. 'servers' is a LIST of servers to connect to.

File heechee/webdav/caching/rediscache.py

+import logging
+import redis
+
+try:
+    from hashlib import md5
+except ImportError:
+    from md5 import md5
+
+class RedisCache(object):
+    """
+    Simple Redis cache.
+    """
+    def __init__(self, server, db=0):
+        self._cache = redis.Redis(host=server, db=db)
+    
+    def __getitem__(self, key):
+        value = self._cache.get(key)
+        if value is not None:
+            logging.debug("cache HIT: %r" % key)
+            return value
+        else:
+            logging.debug("cache MISS: %r" % key)
+            raise KeyError("No such key %r" % key)
+    
+    def __setitem__(self, key, value):
+        logging.debug("cache SET: %r (%i)" % (key, len(value)))
+        self._cache.set(key, value)