Unverified Commit 93f38556 authored by mathieui's avatar mathieui

Add a "remove" action on the cache API

parent 9cab0243
......@@ -18,6 +18,9 @@ class Cache:
def store(self, key, value):
raise NotImplementedError
def remove(self, key):
raise NotImplemented
class PerJidCache:
def retrieve_by_jid(self, jid, key):
raise NotImplementedError
......@@ -25,6 +28,9 @@ class PerJidCache:
def store_by_jid(self, jid, key, value):
raise NotImplementedError
def remove_by_jid(self, jid, key):
raise NotImplementedError
class MemoryCache(Cache):
def __init__(self):
self.cache = {}
......@@ -36,6 +42,11 @@ class MemoryCache(Cache):
self.cache[key] = value
return True
def remove(self, key):
if key in self.cache:
del self.cache[key]
return True
class MemoryPerJidCache(PerJidCache):
def __init__(self):
self.cache = {}
......@@ -51,6 +62,12 @@ class MemoryPerJidCache(PerJidCache):
cache[key] = value
return True
def remove_by_jid(self, jid, key):
cache = self.cache.get(jid, None)
if cache is not None and key in cache:
del cache[key]
return True
class FileSystemStorage:
def __init__(self, encode, decode, binary):
self.encode = encode if encode is not None else lambda x: x
......@@ -80,6 +97,15 @@ class FileSystemStorage:
log.debug('Failed to store %s to cache:', key, exc_info=True)
return False
def _remove(self, directory, key):
filename = os.path.join(directory, key.replace('/', '_'))
try:
os.remove(filename)
except OSError:
log.debug('Failed to remove %s from cache:', key, exc_info=True)
return False
return True
class FileSystemCache(Cache, FileSystemStorage):
def __init__(self, directory, cache_type, *, encode=None, decode=None, binary=False):
FileSystemStorage.__init__(self, encode, decode, binary)
......@@ -91,6 +117,9 @@ class FileSystemCache(Cache, FileSystemStorage):
def store(self, key, value):
return self._store(self.base_dir, key, value)
def remove(self, key):
return self._remove(self.base_dir, key)
class FileSystemPerJidCache(PerJidCache, FileSystemStorage):
def __init__(self, directory, cache_type, *, encode=None, decode=None, binary=False):
FileSystemStorage.__init__(self, encode, decode, binary)
......@@ -103,3 +132,7 @@ class FileSystemPerJidCache(PerJidCache, FileSystemStorage):
def store_by_jid(self, jid, key, value):
directory = os.path.join(self.base_dir, jid)
return self._store(directory, key, value)
def remove_by_jid(self, jid, key):
directory = os.path.join(self.base_dir, jid)
return self._remove(directory, key)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment