Add a "remove" action on the cache API
This commit is contained in:
parent
9cab02438b
commit
93f385562f
1 changed files with 33 additions and 0 deletions
|
@ -18,6 +18,9 @@ class Cache:
|
||||||
def store(self, key, value):
|
def store(self, key, value):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def remove(self, key):
|
||||||
|
raise NotImplemented
|
||||||
|
|
||||||
class PerJidCache:
|
class PerJidCache:
|
||||||
def retrieve_by_jid(self, jid, key):
|
def retrieve_by_jid(self, jid, key):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
@ -25,6 +28,9 @@ class PerJidCache:
|
||||||
def store_by_jid(self, jid, key, value):
|
def store_by_jid(self, jid, key, value):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def remove_by_jid(self, jid, key):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
class MemoryCache(Cache):
|
class MemoryCache(Cache):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.cache = {}
|
self.cache = {}
|
||||||
|
@ -36,6 +42,11 @@ class MemoryCache(Cache):
|
||||||
self.cache[key] = value
|
self.cache[key] = value
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def remove(self, key):
|
||||||
|
if key in self.cache:
|
||||||
|
del self.cache[key]
|
||||||
|
return True
|
||||||
|
|
||||||
class MemoryPerJidCache(PerJidCache):
|
class MemoryPerJidCache(PerJidCache):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.cache = {}
|
self.cache = {}
|
||||||
|
@ -51,6 +62,12 @@ class MemoryPerJidCache(PerJidCache):
|
||||||
cache[key] = value
|
cache[key] = value
|
||||||
return True
|
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:
|
class FileSystemStorage:
|
||||||
def __init__(self, encode, decode, binary):
|
def __init__(self, encode, decode, binary):
|
||||||
self.encode = encode if encode is not None else lambda x: x
|
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)
|
log.debug('Failed to store %s to cache:', key, exc_info=True)
|
||||||
return False
|
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):
|
class FileSystemCache(Cache, FileSystemStorage):
|
||||||
def __init__(self, directory, cache_type, *, encode=None, decode=None, binary=False):
|
def __init__(self, directory, cache_type, *, encode=None, decode=None, binary=False):
|
||||||
FileSystemStorage.__init__(self, encode, decode, binary)
|
FileSystemStorage.__init__(self, encode, decode, binary)
|
||||||
|
@ -91,6 +117,9 @@ class FileSystemCache(Cache, FileSystemStorage):
|
||||||
def store(self, key, value):
|
def store(self, key, value):
|
||||||
return self._store(self.base_dir, 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):
|
class FileSystemPerJidCache(PerJidCache, FileSystemStorage):
|
||||||
def __init__(self, directory, cache_type, *, encode=None, decode=None, binary=False):
|
def __init__(self, directory, cache_type, *, encode=None, decode=None, binary=False):
|
||||||
FileSystemStorage.__init__(self, encode, decode, binary)
|
FileSystemStorage.__init__(self, encode, decode, binary)
|
||||||
|
@ -103,3 +132,7 @@ class FileSystemPerJidCache(PerJidCache, FileSystemStorage):
|
||||||
def store_by_jid(self, jid, key, value):
|
def store_by_jid(self, jid, key, value):
|
||||||
directory = os.path.join(self.base_dir, jid)
|
directory = os.path.join(self.base_dir, jid)
|
||||||
return self._store(directory, key, value)
|
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)
|
||||||
|
|
Loading…
Reference in a new issue