Adding flask_cache

This commit is contained in:
Denis-Cosmin Nutiu 2017-04-21 02:38:07 +03:00
parent fc9c721d96
commit ae1b44d85a
5 changed files with 19 additions and 4 deletions

View file

@ -12,6 +12,7 @@ nose==1.3.7
pbr==2.0.0 pbr==2.0.0
pycodestyle==2.3.1 pycodestyle==2.3.1
pyflakes==1.5.0 pyflakes==1.5.0
pylibmc==1.5.2
PyMySQL==0.7.11 PyMySQL==0.7.11
six==1.10.0 six==1.10.0
SQLAlchemy==1.1.9 SQLAlchemy==1.1.9

View file

@ -23,6 +23,7 @@ except ImportError:
from src.models import db from src.models import db
from src.views.errors import error_pages from src.views.errors import error_pages
from src.views.scoreboard import scoreboard from src.views.scoreboard import scoreboard
from src.resources.utilities import cache
import sys import sys
import flask_bootstrap import flask_bootstrap
import flask import flask
@ -33,6 +34,7 @@ def create_app(config_name):
app.config.from_object(config[config_name]) app.config.from_object(config[config_name])
db.init_app(app) db.init_app(app)
cache.init_app(app)
config[config_name].init_app(app) config[config_name].init_app(app)
flask_bootstrap.Bootstrap(app) flask_bootstrap.Bootstrap(app)
app.register_blueprint(scoreboard) app.register_blueprint(scoreboard)

View file

@ -30,11 +30,14 @@ class Config:
SQLALCHEMY_COMMIT_ON_TEARDOWN = True SQLALCHEMY_COMMIT_ON_TEARDOWN = True
DEBUG = True DEBUG = True
# Server Settings # Server Settings
SERVER_NAME = "localhost:5000" # If not set correctly will generate lots of 404's SERVER_NAME = "localhost:5000"
ADMIN_EMAIL = "admin@localhost.com" ADMIN_EMAIL = "metonymy@fedoraproject.com"
ADMIN_NAME = "WebMaster" ADMIN_NAME = "Metonymy"
APP_IP = "0.0.0.0" APP_IP = "localhost"
APP_PORT = 5000 APP_PORT = 5000
CACHE_TYPE = "memcached"
CACHE_DEFAULT_TIMEOUT = 60
CACHE_KEY_PREFIX = "benchmark_scoreboard"
# Pagination # Pagination
MAX_RESULTS_PER_PAGE = 50 MAX_RESULTS_PER_PAGE = 50
MAX_PAGES = 2 MAX_PAGES = 2
@ -46,6 +49,7 @@ class Config:
class DevelopmentConfig(Config): class DevelopmentConfig(Config):
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'database.sqlite') SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'database.sqlite')
CACHE_TYPE = "simple"
@staticmethod @staticmethod
def init_app(app): def init_app(app):
@ -73,6 +77,7 @@ class ProductionConfig(Config):
class TestingConfig(Config): class TestingConfig(Config):
MAX_RESULTS_PER_PAGE = 1 MAX_RESULTS_PER_PAGE = 1
CACHE_TYPE = "simple"
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'test_database.sqlite') SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'test_database.sqlite')
@staticmethod @staticmethod

View file

@ -16,6 +16,10 @@
along with scoreboard-benchmark . If not, see <http://www.gnu.org/licenses/>. along with scoreboard-benchmark . If not, see <http://www.gnu.org/licenses/>.
""" """
from flask_cache import Cache
cache = Cache()
def to_zero_count(page_no): def to_zero_count(page_no):
""" """

View file

@ -24,6 +24,7 @@ import flask
scoreboard = flask.Blueprint('scoreboard', __name__, template_folder='templates') scoreboard = flask.Blueprint('scoreboard', __name__, template_folder='templates')
@utilities.cache.cached(timeout=60*10)
@scoreboard.route("/upload") @scoreboard.route("/upload")
def upload(): def upload():
""" """
@ -72,6 +73,7 @@ def result_post():
return flask.jsonify({'success': True, 'location': location}), 201, {'location': location} return flask.jsonify({'success': True, 'location': location}), 201, {'location': location}
@utilities.cache.cached(timeout=60*5)
@scoreboard.route("/result/<id>", methods=['GET']) @scoreboard.route("/result/<id>", methods=['GET'])
def result(id): def result(id):
""" """
@ -85,6 +87,7 @@ def result(id):
flask.abort(404) flask.abort(404)
@utilities.cache.cached(timeout=60)
@scoreboard.route("/", methods=['GET', 'POST']) @scoreboard.route("/", methods=['GET', 'POST'])
def index(): def index():
""" """