mirror of
https://github.com/Findus23/RadioStats.git
synced 2024-09-19 16:03:48 +02:00
add redis caching
This commit is contained in:
parent
72d9270880
commit
1c1ba18760
2 changed files with 13 additions and 1 deletions
7
app.py
7
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)
|
||||
|
|
|
@ -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/<channel>')
|
||||
@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/<channel>/plays/<song_id>')
|
||||
@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/<channel>/details/<song_id>')
|
||||
@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()
|
||||
|
|
Loading…
Reference in a new issue