heechee / heechee / webdav / caching /

    import cmemcache as memcache
except ImportError:
    import memcache

    from hashlib import md5
except ImportError:
    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.
    def __init__(self, servers, timeout=600):
        Constructor. 'servers' is a LIST of servers to connect to.
        self._cache = memcache.Client(servers)
        self.timeout = timeout
    def __getitem__(self, key):
        #print "cache TRY: %r" % key
        value = self._cache.get(key)
        if value is not None:
            #print "cache HIT"
            return value
            raise KeyError("No such key %r" % key)
    def __setitem__(self, key, value):
        #print "cache SET: %r (%i)" % (key, len(value))
        self._cache.add(key, value, self.timeout)