diff --git a/requirements/dev.in b/requirements/dev.in index 4a2502f75d7fc0e3df16f15e04449bb91ef71268..5531f6bed571134c38e5ca2cb3d04b00d2ec5caa 100644 --- a/requirements/dev.in +++ b/requirements/dev.in @@ -4,6 +4,7 @@ Flask-Cors>=3.0.10 Flask-BS4>=4.5.3.0,<5.0 Flask-Admin>=1.5.7 Flask-Login>=0.5.0 +Flask-Caching>=1.10.1 celery[redis]>=5.0.5 PyMySQL>=1.0.2 psycopg2-binary>=2.8.6 diff --git a/src/covid19/blueprints/app_all/all_model.py b/src/covid19/blueprints/app_all/all_model.py index d5d7858548c966ea15b830e4987e03941bdddf07..909779d51dcc8d3d026a9c4386d5c4b3fc96ba2d 100644 --- a/src/covid19/blueprints/app_all/all_model.py +++ b/src/covid19/blueprints/app_all/all_model.py @@ -1,5 +1,5 @@ from datetime import date, datetime -from database import db, ITEMS_PER_PAGE +from database import db, cache, ITEMS_PER_PAGE class ApplicationDateReported(db.Model): @@ -136,12 +136,14 @@ class ApplicationDateReported(db.Model): .one_or_none() @classmethod + @cache.memoize(50) def get_by_date_reported(cls, p_date_reported: str): return db.session.query(cls)\ .filter(cls.date_reported_import_str == p_date_reported)\ .one() @classmethod + @cache.memoize(50) def find_by_date_reported(cls, p_date_reported: str): return db.session.query(cls)\ .filter(cls.date_reported_import_str == p_date_reported)\ @@ -206,6 +208,7 @@ class ApplicationRegion(db.Model): return regions @classmethod + @cache.memoize(50) def get_by_id(cls, other_id: int): return db.session.query(cls)\ .filter(cls.id == other_id)\ @@ -218,6 +221,7 @@ class ApplicationRegion(db.Model): .one_or_none() @classmethod + @cache.memoize(50) def get_by_region(cls, i_region: str): return db.session.query(cls)\ .filter(cls.region == i_region)\ diff --git a/src/covid19/blueprints/who/who_model.py b/src/covid19/blueprints/who/who_model.py index 6340ab3ffbea9c120fd23b15a031fb795f5885ce..35afafcee9ee01e5904f86f446604cac7905f7e9 100644 --- a/src/covid19/blueprints/who/who_model.py +++ b/src/covid19/blueprints/who/who_model.py @@ -1,7 +1,7 @@ from sqlalchemy import and_ from datetime import date from sqlalchemy.orm import joinedload, subqueryload -from database import db, ITEMS_PER_PAGE +from database import db, cache, ITEMS_PER_PAGE from covid19.blueprints.app_all.all_model import ApplicationDateReported, ApplicationRegion @@ -139,6 +139,7 @@ class WhoCountry(db.Model): ).one_or_none() @classmethod + @cache.memoize(50) def get_by_country_code(cls, i_country_code): return db.session.query(cls).filter( cls.country_code == i_country_code @@ -150,13 +151,14 @@ class WhoCountry(db.Model): cls.country == i_country ).one_or_none() - @classmethod - def get_by_country_code(cls, i_country_code): - return db.session.query(cls).filter( - cls.country_code == i_country_code - ).one() + # @classmethod + # def get_by_country_code(cls, i_country_code): + # return db.session.query(cls).filter( + # cls.country_code == i_country_code + # ).one() @classmethod + @cache.memoize(50) def get_by_country(cls, i_country): return db.session.query(cls).filter( cls.country == i_country diff --git a/src/database.py b/src/database.py index 1970a40c3f561e6e3eeb5c6aca979a89a80f12da..1553a35ddbfba1b6b8c9809795278d3ec329c3b0 100644 --- a/src/database.py +++ b/src/database.py @@ -1,6 +1,6 @@ # TODO: #210 database.py: logging for Celery on Windows from flask import Flask, logging -# from flask_caching import Cache +from flask_caching import Cache from flask_cors import CORS from flask_bs4 import Bootstrap from flask_sqlalchemy import SQLAlchemy @@ -11,7 +11,9 @@ from celery import Celery # TODO: #210 database.py: logging for Celery on Windows from celery.utils.log import LoggingProxy -# cache = Cache(config={"CACHE_TYPE": "simple"}) +# https://flask-caching.readthedocs.io/en/latest/ + +cache = Cache(config={"CACHE_TYPE": "simple"}) app_cors = CORS() app_bootstrap = Bootstrap() login_manager = LoginManager() @@ -32,7 +34,7 @@ def create_app(): my_app.config['SQLALCHEMY_DATABASE_URI'] = my_db_url my_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # silence the deprecation warning my_app.config['FLASK_ADMIN_SWATCH'] = 'superhero' - # cache.init_app(app) + cache.init_app(my_app) return my_app