Add Redis cache backend

master
pictuga 2021-09-11 21:36:14 +02:00
parent 62e04549ac
commit 7d86972e58
3 changed files with 30 additions and 3 deletions

View File

@ -60,8 +60,8 @@ Full installation (including optional dependencies)
pip install git+https://git.pictuga.com/pictuga/morss.git#[full] pip install git+https://git.pictuga.com/pictuga/morss.git#[full]
``` ```
The full install includes mysql (possible cache backend). Otherwise, only The full install includes mysql and redis (possible cache backends). Otherwise,
in-memory and sqlite3 caches are available. only in-memory and sqlite3 caches are available.
The dependency `lxml` is fairly long to install (especially on Raspberry Pi, as The dependency `lxml` is fairly long to install (especially on Raspberry Pi, as
C code needs to be compiled). If possible on your distribution, try installing C code needs to be compiled). If possible on your distribution, try installing
@ -372,6 +372,8 @@ will be cleared every time the program is run). Path can be defined with
`SQLITE_PATH`. `SQLITE_PATH`.
- `CACHE=mysql`: MySQL cache. Connection can be defined with the following - `CACHE=mysql`: MySQL cache. Connection can be defined with the following
environment variables: `MYSQL_USER`, `MYSQL_PWD`, `MYSQL_DB`, `MYSQL_HOST` environment variables: `MYSQL_USER`, `MYSQL_PWD`, `MYSQL_DB`, `MYSQL_HOST`
- `CACHE=redis`: Redis cache. Connection can be defined with the following
environment variables: `REDIS_HOST`, `REDIS_PORT`, `REDIS_DB`, `REDIS_PWD`
To limit the size of the cache: To limit the size of the cache:

View File

@ -141,6 +141,23 @@ class CappedDict(OrderedDict, BaseCache):
OrderedDict.__setitem__(self, key, data) OrderedDict.__setitem__(self, key, data)
try:
import redis # isort:skip
except ImportError:
pass
class RedisCacheHandler(BaseCache):
def __init__(self, host='localhost', port=6379, db=0, password=None):
self.r = redis.Redis(host=host, port=port, db=db, password=password)
def __getitem__(self, key):
return self.r.get(key)
def __setitem__(self, key, data):
self.r.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(
@ -159,5 +176,13 @@ if 'CACHE' in os.environ:
default_cache = SQLiteCache(path) default_cache = SQLiteCache(path)
elif os.environ['CACHE'] == 'redis':
default_cache = RedisCacheHandler(
host = os.getenv('REDIS_HOST', 'localhost'),
port = int(os.getenv('REDIS_PORT', 6379)),
db = int(os.getenv('REDIS_DB', 0)),
password = os.getenv('REDIS_PWD', None)
)
else: else:
default_cache = CappedDict() default_cache = CappedDict()

View File

@ -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']}, extras_require = {'full': ['pymysql', 'redis']},
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']),