diff --git a/api/server/swagger_server/controllers/paper_details_controller.py b/api/server/swagger_server/controllers/paper_details_controller.py index 93620fef..02f17cec 100644 --- a/api/server/swagger_server/controllers/paper_details_controller.py +++ b/api/server/swagger_server/controllers/paper_details_controller.py @@ -1,4 +1,5 @@ from math import ceil +from neoflask.cache import cache from neoflask.queries import REFEREED_PREPRINTS_V2 from neotools import ask_neo from urllib.parse import urlencode @@ -27,7 +28,7 @@ def papers_url(reviewed_by=None, query=None, page=None, per_page=None, sort_by=N query_params = urlencode(query_params_list) return f"{base_url}?{query_params}" - +@cache.memoize() # memoize handles function parameters, .cached does not def papers_get(reviewed_by=None, query=None, page=None, per_page=None, sort_by=None, sort_order=None): # noqa: E501 """Get paginated collections of papers, optionally filtered by reviewing service diff --git a/api/server/swagger_server/controllers/reviewing_services_controller.py b/api/server/swagger_server/controllers/reviewing_services_controller.py index 44049a05..34e86da4 100644 --- a/api/server/swagger_server/controllers/reviewing_services_controller.py +++ b/api/server/swagger_server/controllers/reviewing_services_controller.py @@ -1,7 +1,8 @@ +from neoflask.cache import cache from neoflask.queries import DESCRIBE_REVIEWING_SERVICES_V2 from neotools import ask_neo - +@cache.cached() def reviewing_services_get(): # noqa: E501 """Get information about available reviewing services diff --git a/neoflask/__init__.py b/neoflask/__init__.py index d6c1f2ea..3e4d1260 100644 --- a/neoflask/__init__.py +++ b/neoflask/__init__.py @@ -1,10 +1,9 @@ import os from connexion import FlaskApp from dotenv import load_dotenv -from neotools.db import Instance +from .cache import init_cache from .config import Config from flask_cors import CORS -from flask_caching import Cache from swagger_server import encoder @@ -27,22 +26,6 @@ Config.init_app(app) app.config.from_object(Config) -cache = Cache(config={ - 'CACHE_DEFAULT_TIMEOUT': 365 * 24 * 60 * 60, - 'CACHE_KEY_PREFIX': __name__, - 'CACHE_TYPE': 'redis', - 'CACHE_REDIS_HOST': 'redis', - 'CACHE_REDIS_PORT': '6379', - # 'CACHE_THRESHOLD': 1000, - # 'CACHE_REDIS_PASSWORD': '', - # 'CACHE_REDIS_DB': '', - # 'CACHE_ARGS': '', - # 'CACHE_OPTIONS': '', - # 'CACHE_REDIS_URL': '', -}) - -cache.init_app(app) -with app.app_context(): - cache.clear() +init_cache(app) from . import views diff --git a/neoflask/cache.py b/neoflask/cache.py new file mode 100644 index 00000000..b241db2d --- /dev/null +++ b/neoflask/cache.py @@ -0,0 +1,15 @@ +from flask_caching import Cache + +cache = Cache(config={ + 'CACHE_DEFAULT_TIMEOUT': 365 * 24 * 60 * 60, + 'CACHE_KEY_PREFIX': __name__, + 'CACHE_TYPE': 'redis', + 'CACHE_REDIS_HOST': 'redis', + 'CACHE_REDIS_PORT': '6379', +}) + + +def init_cache(app): + cache.init_app(app) + with app.app_context(): + cache.clear() diff --git a/neoflask/views.py b/neoflask/views.py index a56bf687..3ac132e1 100644 --- a/neoflask/views.py +++ b/neoflask/views.py @@ -1,7 +1,6 @@ from collections import namedtuple from datetime import date, timedelta from functools import wraps -import pdb from dateutil.relativedelta import relativedelta from flask import ( abort, @@ -26,9 +25,10 @@ COLLECTION_NAMES, SUBJECT_COLLECTIONS, ) +from neoflask.cache import cache from neotools import ask_neo import re -from . import app, cache +from . import app DOI_REGEX = re.compile(r'10.\d{4,9}/[-._;()/:A-Z0-9]+$', flags=re.IGNORECASE)