Try the simplest flask cache possible.

This commit is contained in:
2021-03-07 19:10:48 +01:00
parent 43f27a4643
commit 7039489838
3 changed files with 14 additions and 4 deletions
+9
View File
@@ -15,11 +15,16 @@
import bleach import bleach
import os import os
from flask import Flask from flask import Flask
from flask_caching import Cache
from flaskext.markdown import Markdown from flaskext.markdown import Markdown
from markdown.extensions.wikilinks import WikiLinkExtension from markdown.extensions.wikilinks import WikiLinkExtension
from . import util from . import util
from flask_cors import CORS 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): def wikilink_to_url(label, base, end):
label = util.canonical_wikilink(label) label = util.canonical_wikilink(label)
url = '/node/' + label url = '/node/' + label
@@ -30,8 +35,12 @@ def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True) app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping( app.config.from_mapping(
SECRET_KEY='dev', SECRET_KEY='dev',
DEBUG=True, # some Flask specific configs
CACHE_TYPE="SimpleCache", # Flask-Caching related configs
CACHE_DEFAULT_TIMEOUT=30
) )
CORS(app) CORS(app)
cache.init_app(app, config={'CACHE_TYPE': 'SimpleCache', 'CACHE_DEFAULT_TIMEOUT': 30})
if test_config is None: if test_config is None:
# load the instance config, if it exists, when not testing # load the instance config, if it exists, when not testing
+4 -4
View File
@@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import cachetools.func
import glob import glob
import itertools import itertools
import re import re
import os import os
from flask import current_app from flask import current_app
from . import cache
from . import config from . import config
from . import feed from . import feed
from . import render from . import render
@@ -80,7 +80,7 @@ class Graph:
nodes = [node for node in G.nodes() if node.wikilink in permutations and node.subnodes] nodes = [node for node in G.nodes() if node.wikilink in permutations and node.subnodes]
return nodes return nodes
@cachetools.func.ttl_cache(maxsize=2, ttl=20) @cache.memoize(timeout=30)
def nodes(self, include_journals=True): def nodes(self, include_journals=True):
current_app.logger.debug('Loading graph.') current_app.logger.debug('Loading graph.')
# returns a list of all nodes # returns a list of all nodes
@@ -112,7 +112,7 @@ class Graph:
# Running something like this would be ideal eventually though. # 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, # It might also work better once all pulling/pushing logic moves to Graph, where it belongs,
# and can make use of more sensible algorithms. # 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): def compute_transclusion(self, include_journals=True):
# Add artisanal virtual subnodes (resulting from transclusion/[[push]]) to all nodes. # Add artisanal virtual subnodes (resulting from transclusion/[[push]]) to all nodes.
@@ -121,7 +121,7 @@ class Graph:
node.subnodes.extend(pushed_subnodes) node.subnodes.extend(pushed_subnodes)
# does this belong here? # 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()): def subnodes(self, sort=lambda x: x.uri.lower()):
# Markdown. # Markdown.
subnodes = [Subnode(f) for f in glob.glob(os.path.join(config.AGORA_PATH, '**/*.md'), recursive=True)] subnodes = [Subnode(f) for f in glob.glob(os.path.join(config.AGORA_PATH, '**/*.md'), recursive=True)]
+1
View File
@@ -5,6 +5,7 @@ dateparser==1.0.0
feedparser==6.0.2 feedparser==6.0.2
filetype==1.0.7 filetype==1.0.7
Flask==1.1.2 Flask==1.1.2
Flask-Caching==1.10.0
Flask-Cors==3.0.10 Flask-Cors==3.0.10
Flask-Markdown==0.3 Flask-Markdown==0.3
Flask-WTF==0.14.3 Flask-WTF==0.14.3