From 7039489838ef2e64c38898ccf290270dcda78e51 Mon Sep 17 00:00:00 2001 From: Flancian <0@flancia.org> Date: Sun, 7 Mar 2021 19:10:48 +0100 Subject: [PATCH] Try the simplest flask cache possible. --- app/__init__.py | 9 +++++++++ app/db.py | 8 ++++---- requirements.txt | 1 + 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index e00921f28..75654c70d 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -15,11 +15,16 @@ import bleach import os from flask import Flask +from flask_caching import Cache from flaskext.markdown import Markdown from markdown.extensions.wikilinks import WikiLinkExtension from . import util from flask_cors import CORS +cache = Cache() + +# what is this doing here, I have no idea. +# todo: move to util. def wikilink_to_url(label, base, end): label = util.canonical_wikilink(label) url = '/node/' + label @@ -30,8 +35,12 @@ def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY='dev', + DEBUG=True, # some Flask specific configs + CACHE_TYPE="SimpleCache", # Flask-Caching related configs + CACHE_DEFAULT_TIMEOUT=30 ) CORS(app) + cache.init_app(app, config={'CACHE_TYPE': 'SimpleCache', 'CACHE_DEFAULT_TIMEOUT': 30}) if test_config is None: # load the instance config, if it exists, when not testing diff --git a/app/db.py b/app/db.py index 9b9729f88..2eeeb454c 100644 --- a/app/db.py +++ b/app/db.py @@ -12,12 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import cachetools.func import glob import itertools import re import os from flask import current_app +from . import cache from . import config from . import feed from . import render @@ -80,7 +80,7 @@ class Graph: nodes = [node for node in G.nodes() if node.wikilink in permutations and node.subnodes] return nodes - @cachetools.func.ttl_cache(maxsize=2, ttl=20) + @cache.memoize(timeout=30) def nodes(self, include_journals=True): current_app.logger.debug('Loading graph.') # returns a list of all nodes @@ -112,7 +112,7 @@ class Graph: # Running something like this would be ideal eventually though. # It might also work better once all pulling/pushing logic moves to Graph, where it belongs, # and can make use of more sensible algorithms. - @cachetools.func.ttl_cache(maxsize=2, ttl=20) + @cache.memoize(timeout=30) def compute_transclusion(self, include_journals=True): # Add artisanal virtual subnodes (resulting from transclusion/[[push]]) to all nodes. @@ -121,7 +121,7 @@ class Graph: node.subnodes.extend(pushed_subnodes) # does this belong here? - @cachetools.func.ttl_cache(maxsize=1, ttl=20) + @cache.memoize(timeout=30) def subnodes(self, sort=lambda x: x.uri.lower()): # Markdown. subnodes = [Subnode(f) for f in glob.glob(os.path.join(config.AGORA_PATH, '**/*.md'), recursive=True)] diff --git a/requirements.txt b/requirements.txt index 8913eb507..418af5635 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ dateparser==1.0.0 feedparser==6.0.2 filetype==1.0.7 Flask==1.1.2 +Flask-Caching==1.10.0 Flask-Cors==3.0.10 Flask-Markdown==0.3 Flask-WTF==0.14.3