Jesper Nøhr avatar Jesper Nøhr committed 3f47d73

adding redis cache, fixing docstring in memcacher

Comments (0)

Files changed (2)

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.

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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.