Skip to content

Commit

Permalink
Added caching to v2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
eidens committed Dec 8, 2023
1 parent 5f7f324 commit eff10bb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 2 additions & 19 deletions neoflask/__init__.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
15 changes: 15 additions & 0 deletions neoflask/cache.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 2 additions & 2 deletions neoflask/views.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit eff10bb

Please sign in to comment.