caching: add diskcache backend
parent
cbf7b3f77b
commit
8de309f2d4
|
@ -158,6 +158,26 @@ class RedisCacheHandler(BaseCache):
|
||||||
self.r.set(key, data)
|
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 'CACHE' in os.environ:
|
||||||
if os.environ['CACHE'] == 'mysql':
|
if os.environ['CACHE'] == 'mysql':
|
||||||
default_cache = MySQLCacheHandler(
|
default_cache = MySQLCacheHandler(
|
||||||
|
@ -180,5 +200,11 @@ if 'CACHE' in os.environ:
|
||||||
password = os.getenv('REDIS_PWD', None)
|
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:
|
else:
|
||||||
default_cache = CappedDict()
|
default_cache = CappedDict()
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -14,7 +14,7 @@ setup(
|
||||||
license = 'AGPL v3',
|
license = 'AGPL v3',
|
||||||
packages = [package_name],
|
packages = [package_name],
|
||||||
install_requires = ['lxml', 'bs4', 'python-dateutil', 'chardet'],
|
install_requires = ['lxml', 'bs4', 'python-dateutil', 'chardet'],
|
||||||
extras_require = {'full': ['pymysql', 'redis']},
|
extras_require = {'full': ['pymysql', 'redis', 'diskcache']},
|
||||||
package_data = {package_name: ['feedify.ini']},
|
package_data = {package_name: ['feedify.ini']},
|
||||||
data_files = [
|
data_files = [
|
||||||
('share/' + package_name, ['README.md', 'LICENSE']),
|
('share/' + package_name, ['README.md', 'LICENSE']),
|
||||||
|
|
Loading…
Reference in New Issue