From 8de309f2d4c9b2e5551c621b13e589048e80c72c Mon Sep 17 00:00:00 2001 From: pictuga Date: Sun, 7 Nov 2021 18:15:20 +0100 Subject: [PATCH] caching: add diskcache backend --- morss/caching.py | 26 ++++++++++++++++++++++++++ setup.py | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/morss/caching.py b/morss/caching.py index 063c4e4..b5f55d6 100644 --- a/morss/caching.py +++ b/morss/caching.py @@ -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() diff --git a/setup.py b/setup.py index 40e22cf..404955e 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ setup( license = 'AGPL v3', packages = [package_name], install_requires = ['lxml', 'bs4', 'python-dateutil', 'chardet'], - extras_require = {'full': ['pymysql', 'redis']}, + extras_require = {'full': ['pymysql', 'redis', 'diskcache']}, package_data = {package_name: ['feedify.ini']}, data_files = [ ('share/' + package_name, ['README.md', 'LICENSE']),