From d92cd6f3a217e8cfd587f6f53630577416c34e7f Mon Sep 17 00:00:00 2001 From: Bui Date: Thu, 14 Feb 2019 10:31:23 +0000 Subject: [PATCH] add view caching --- app.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app.py b/app.py index d2512f7..1a805b8 100644 --- a/app.py +++ b/app.py @@ -4,12 +4,19 @@ from __future__ import division import json import sqlite3 +import tempfile import re import os from flask import Flask, g, jsonify, render_template, request, abort, redirect +from flask_caching import Cache from ffmpy import FFmpeg app = Flask(__name__) +try: + app.cache = Cache(app, config={'CACHE_TYPE': 'redis'}) +except RuntimeError: + app.cache = Cache(app, config={'CACHE_TYPE': 'filesystem', 'CACHE_DIR': tempfile.gettempdir()}) + DATABASE = 'taiko.db' DEFAULT_URL = 'https://github.com/bui/taiko-web/' @@ -149,6 +156,7 @@ def route_index(): @app.route('/api/preview') +@app.cache.cached(timeout=15) def route_api_preview(): song_id = request.args.get('id', None) if not song_id or not re.match('^[0-9]+$', song_id): @@ -167,6 +175,7 @@ def route_api_preview(): @app.route('/api/songs') +@app.cache.cached(timeout=15) def route_api_songs(): songs = query_db('select * from songs where enabled = 1') @@ -210,6 +219,7 @@ def route_api_songs(): @app.route('/api/config') +@app.cache.cached(timeout=15) def route_api_config(): config = get_config() return jsonify(config)