diff --git a/app.py b/app.py index 1063eea..f019579 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,7 @@ import logging from flask import Flask +from flask_caching import Cache from playhouse.flask_utils import FlaskDB from playhouse.pool import PooledMySQLDatabase from raven.contrib.flask import Sentry @@ -10,10 +11,16 @@ from raven.contrib.flask import Sentry import config DATABASE = PooledMySQLDatabase("radio", **config.db) +if config.cache: + CACHE_TYPE = "redis" +else: + CACHE_TYPE = "null" +CACHE_REDIS_DB = config.redisDB # Create a Flask WSGI app and configure it using values from the module. app = Flask(__name__) app.config.from_object(__name__) +cache = Cache(app) if config.sentryDSN: sentry = Sentry(app, dsn=config.sentryDSN, logging=True, level=logging.WARNING) diff --git a/server.py b/server.py index 01bf4c6..39eb48a 100644 --- a/server.py +++ b/server.py @@ -4,7 +4,7 @@ from datetime import datetime, timedelta from flask import jsonify, request from playhouse.shortcuts import model_to_dict -from app import app +from app import app, cache from models import * @@ -50,6 +50,7 @@ def query_to_response(query, limit=10, key=False, sort=False, offset=None, list= @app.route('/api/') +@cache.cached(60 * 60 * 24) def index(): return query_to_response(Channel.select(), limit=False, key="shortname") @@ -84,6 +85,7 @@ def get_dates_from_request(): @app.route('/api/') +@cache.cached(60 * 15) def popular(channel): date, date_type = get_dates_from_request() start, end = get_range(date, date_type) @@ -104,6 +106,7 @@ def popular(channel): @app.route('/api//plays/') +@cache.cached(60 * 15) def plays(channel, song_id): date, date_type = get_dates_from_request() start, end = get_range(date, date_type) @@ -117,6 +120,7 @@ def plays(channel, song_id): @app.route('/api//details/') +@cache.cached(60 * 15) def details(channel, song_id): song_get = Song.select() \ .where(Song.id == song_id) @@ -135,6 +139,7 @@ def details(channel, song_id): @app.route('/api/stats') +@cache.cached(60 * 60) def stats(): total_num_plays = Play.select().count() total_num_unique_songs = Song.select().where(Song.show == 0).count()