@@ -2,6 +2,7 @@
|
||||
requests = "*"
|
||||
pyopenmensa = "*"
|
||||
flask = "*"
|
||||
cachetools = "*"
|
||||
|
||||
[dev-packages]
|
||||
pytest = "*"
|
||||
|
||||
Generated
+9
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "4c66b42d8352700feffd46dae3c934f746212df7e897b570715bd799c3c5f121"
|
||||
"sha256": "475551fbc4e977aa1a4edffb7f8549bf4046075e632ac6908f73e3a7a6e0d6bf"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
@@ -16,6 +16,14 @@
|
||||
]
|
||||
},
|
||||
"default": {
|
||||
"cachetools": {
|
||||
"hashes": [
|
||||
"sha256:1d057645db16ca7fe1f3bd953558897603d6f0b9c51ed9d11eb4d071ec4e2aab",
|
||||
"sha256:de5d88f87781602201cde465d3afe837546663b168e8b39df67411b0bf10cefc"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==4.1.0"
|
||||
},
|
||||
"certifi": {
|
||||
"hashes": [
|
||||
"sha256:1d987a998c75633c40847cc966fcf5904906c920a7f17ef374f5aa4282abd304",
|
||||
|
||||
+10
-13
@@ -3,9 +3,10 @@
|
||||
import os
|
||||
import urllib.parse
|
||||
|
||||
import cachetools as ct
|
||||
|
||||
from flask import Flask, jsonify, make_response, url_for
|
||||
from flask.logging import create_logger
|
||||
from werkzeug.contrib.cache import SimpleCache
|
||||
|
||||
from stw_potsdam import feed
|
||||
from stw_potsdam.config import read_canteen_config
|
||||
@@ -29,7 +30,7 @@ if 'BASE_URL' in os.environ: # pragma: no cover
|
||||
if base_url.path:
|
||||
app.config['APPLICATION_ROOT'] = base_url.path
|
||||
|
||||
cache = SimpleCache()
|
||||
cache = ct.ttl.TTLCache(maxsize=30, ttl=CACHE_TIMEOUT)
|
||||
|
||||
|
||||
def canteen_not_found(config, canteen_name):
|
||||
@@ -40,19 +41,15 @@ def canteen_not_found(config, canteen_name):
|
||||
return make_response(message, 404)
|
||||
|
||||
|
||||
def get_menu_cached(canteen):
|
||||
params = MenuParams(canteen_id=canteen.id, chash=canteen.chash)
|
||||
menu = cache.get(params)
|
||||
if menu:
|
||||
log.info('Using cached menu for %s', canteen)
|
||||
return menu or get_menu(canteen, params)
|
||||
def _menu_params(canteen):
|
||||
return MenuParams(canteen_id=canteen.id, chash=canteen.chash)
|
||||
|
||||
|
||||
def get_menu(canteen, params):
|
||||
@ct.cached(cache=cache, key=_menu_params)
|
||||
def get_menu(canteen):
|
||||
log.info('Downloading menu for %s', canteen)
|
||||
menu = download_menu(params)
|
||||
cache.set(params, menu, timeout=CACHE_TIMEOUT)
|
||||
return menu
|
||||
params = _menu_params(canteen)
|
||||
return download_menu(params)
|
||||
|
||||
|
||||
def _canteen_feed_xml(xml):
|
||||
@@ -94,7 +91,7 @@ def canteen_menu_feed(canteen_name):
|
||||
return canteen_not_found(config, canteen_name)
|
||||
|
||||
canteen = config[canteen_name]
|
||||
menu = get_menu_cached(canteen)
|
||||
menu = get_menu(canteen)
|
||||
return canteen_menu_feed_xml(menu)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user