From 80c9d580eb6c10ae50ba49882168c220e00b0390 Mon Sep 17 00:00:00 2001 From: Nicolas F Date: Sun, 11 Aug 2019 03:53:57 +0200 Subject: [PATCH] Improve cache configuration (#564) The Flask-Caching cache can now properly be configured from the config.py, and redis caching has experimentally been tested and confirmed to be working in theory. We also document that one may want to use CACHE_THRESHOLD to limit the maximum number of items in the simple cache. --- config.example.py | 17 +++++++++++++++++ nyaa/__init__.py | 2 +- nyaa/extensions.py | 2 +- requirements.txt | 1 + 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/config.example.py b/config.example.py index 4ca314f..8107de2 100644 --- a/config.example.py +++ b/config.example.py @@ -188,3 +188,20 @@ TRUSTED_MIN_UPLOADS = 10 TRUSTED_MIN_DOWNLOADS = 10000 # Number of days an applicant needs to wait before re-applying TRUSTED_REAPPLY_COOLDOWN = 90 + +########### +## Cache ## +########### + +# Interesting types include "simple", "redis" and "uwsgi" +# See https://pythonhosted.org/Flask-Caching/#configuring-flask-caching +CACHE_TYPE = "simple" + +# Maximum number of items the cache will store +# Only applies to "simple" and "filesystem" cache types +CACHE_THRESHOLD = 8192 + +# If you want to use redis, try this +# CACHE_TYPE = "redis" +# CACHE_REDIS_HOST = "127.0.0.1" +# CACHE_KEY_PREFIX = "catcache_" diff --git a/nyaa/__init__.py b/nyaa/__init__.py index 92aca21..f8830d1 100644 --- a/nyaa/__init__.py +++ b/nyaa/__init__.py @@ -126,6 +126,6 @@ def create_app(config): app.config['DEFAULT_GRAVATAR_URL'] = url # Cache - cache.init_app(app) + cache.init_app(app, config=app.config) return app diff --git a/nyaa/extensions.py b/nyaa/extensions.py index 384a2ea..6281007 100644 --- a/nyaa/extensions.py +++ b/nyaa/extensions.py @@ -10,7 +10,7 @@ from flask_sqlalchemy import BaseQuery, Pagination, SQLAlchemy assets = Environment() db = SQLAlchemy() toolbar = DebugToolbarExtension() -cache = Cache(config={'CACHE_TYPE': 'simple'}) +cache = Cache() class LimitedPagination(Pagination): diff --git a/requirements.txt b/requirements.txt index d8a7420..214354c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -47,6 +47,7 @@ SQLAlchemy-Utils==0.34.1 statsd==3.3.0 urllib3==1.25.3 uWSGI==2.0.18 +redis==3.2.1 webassets==0.12.1 Werkzeug==0.15.5 WTForms==2.2.1