caching: add diskcache backend

This commit is contained in:
2021-11-07 18:15:20 +01:00
parent cbf7b3f77b
commit 8de309f2d4
2 changed files with 27 additions and 1 deletions

View File

@@ -158,6 +158,26 @@ class RedisCacheHandler(BaseCache):
self.r.set(key, data)
try:
import diskcache # isort:skip
except ImportError:
pass
class DiskCacheHandler(BaseCache):
def __init__(self, directory=None, **kwargs):
self.cache = diskcache.Cache(directory=directory, eviction_policy='least-frequently-used', **kwargs)
def trim(self):
self.cache.cull()
def __getitem__(self, key):
return self.cache['key']
def __setitem__(self, key, data):
self.cache.set(key, data)
if 'CACHE' in os.environ:
if os.environ['CACHE'] == 'mysql':
default_cache = MySQLCacheHandler(
@@ -180,5 +200,11 @@ if 'CACHE' in os.environ:
password = os.getenv('REDIS_PWD', None)
)
elif os.environ['CACHE'] == 'diskcache':
default_cache = DiskCacheHandler(
directory = os.getenv('DISKCAHE_DIR', '/tmp/morss-diskcache'),
size_limit = CACHE_SIZE * 102400 # assuming 1 cache item is 100kiB
)
else:
default_cache = CappedDict()