feat: provide own module cache
This commit is contained in:
parent
3c22ad7eec
commit
a9ea9dfaee
4 changed files with 8 additions and 17 deletions
2
app.py
2
app.py
|
@ -1,7 +1,9 @@
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from threads import threads
|
from threads import threads
|
||||||
|
from cache import cache
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
cache.init_app(app, config={'CACHE_TYPE': 'SimpleCache'})
|
||||||
app.register_blueprint(threads, url_prefix='/')
|
app.register_blueprint(threads, url_prefix='/')
|
||||||
|
|
||||||
|
|
||||||
|
|
2
cache.py
Normal file
2
cache.py
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
from flask_caching import Cache
|
||||||
|
cache = Cache()
|
|
@ -1,3 +1,4 @@
|
||||||
flask
|
flask
|
||||||
requests
|
requests
|
||||||
markdown
|
markdown
|
||||||
|
Flask-Caching
|
20
threads.py
20
threads.py
|
@ -3,21 +3,7 @@ import requests
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import markdown
|
import markdown
|
||||||
import re
|
import re
|
||||||
|
from cache import cache
|
||||||
##### Use app provided cache
|
|
||||||
try:
|
|
||||||
from cache.cache import cache
|
|
||||||
except ImportError:
|
|
||||||
print(' ! ERR: Cache module not found')
|
|
||||||
|
|
||||||
def conditional_cache(dec):
|
|
||||||
def decorator(func):
|
|
||||||
if cache is None:
|
|
||||||
return func
|
|
||||||
return dec(func)
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
##########################################
|
|
||||||
|
|
||||||
threads = Blueprint('threads', __name__, template_folder='templates')
|
threads = Blueprint('threads', __name__, template_folder='templates')
|
||||||
|
|
||||||
|
@ -81,7 +67,7 @@ def api():
|
||||||
def api_thread(id):
|
def api_thread(id):
|
||||||
return fetch_thread(id)
|
return fetch_thread(id)
|
||||||
|
|
||||||
@conditional_cache(cache.cached(timeout=60))
|
@cache.cached(timeout=300)
|
||||||
def fetch_statuses():
|
def fetch_statuses():
|
||||||
statuses = []
|
statuses = []
|
||||||
for id in thread_ids:
|
for id in thread_ids:
|
||||||
|
@ -90,7 +76,7 @@ def fetch_statuses():
|
||||||
statuses.append(status)
|
statuses.append(status)
|
||||||
return statuses
|
return statuses
|
||||||
|
|
||||||
@conditional_cache(cache.cached(timeout=60))
|
@cache.cached(timeout=300)
|
||||||
def fetch_thread(id):
|
def fetch_thread(id):
|
||||||
status = requests.get(server + '/api/v1/statuses/' + id ).json()
|
status = requests.get(server + '/api/v1/statuses/' + id ).json()
|
||||||
status = clean_status(status)
|
status = clean_status(status)
|
||||||
|
|
Loading…
Reference in a new issue