summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2019-02-02 17:31:48 +0100
committermathieui <mathieui@mathieui.net>2019-02-02 17:31:48 +0100
commit93f385562ff316142121f520ee344f3accea8487 (patch)
tree1d16b337ab1b7e4996aeeb345c0a3fcc0a501459
parent9cab02438be626d38340d39a9870549b4918e790 (diff)
downloadslixmpp-93f385562ff316142121f520ee344f3accea8487.tar.gz
slixmpp-93f385562ff316142121f520ee344f3accea8487.tar.bz2
slixmpp-93f385562ff316142121f520ee344f3accea8487.tar.xz
slixmpp-93f385562ff316142121f520ee344f3accea8487.zip
Add a "remove" action on the cache API
-rw-r--r--slixmpp/util/cache.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/slixmpp/util/cache.py b/slixmpp/util/cache.py
index 2a117eb4..055163d1 100644
--- a/slixmpp/util/cache.py
+++ b/slixmpp/util/cache.py
@@ -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)