mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-13 09:50:08 -04:00
Merge pull request #3480 from alphagov/request-cache
Import request cache from utils
This commit is contained in:
@@ -2,6 +2,11 @@ from flask import abort, has_request_context, request
|
||||
from flask_login import current_user
|
||||
from notifications_python_client import __version__
|
||||
from notifications_python_client.base import BaseAPIClient
|
||||
from notifications_utils.clients.redis import RequestCache
|
||||
|
||||
from app.extensions import redis_client
|
||||
|
||||
cache = RequestCache(redis_client)
|
||||
|
||||
|
||||
def _attach_current_user(data):
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
import json
|
||||
from contextlib import suppress
|
||||
from datetime import timedelta
|
||||
from functools import wraps
|
||||
from inspect import signature
|
||||
|
||||
from app.extensions import redis_client
|
||||
|
||||
TTL = int(timedelta(days=7).total_seconds())
|
||||
|
||||
|
||||
def _get_argument(argument_name, client_method, args, kwargs):
|
||||
|
||||
with suppress(KeyError):
|
||||
return kwargs[argument_name]
|
||||
|
||||
with suppress(ValueError, IndexError):
|
||||
argument_index = list(signature(client_method).parameters).index(argument_name)
|
||||
return args[argument_index - 1] # -1 because `args` doesn’t include `self`
|
||||
|
||||
with suppress(KeyError):
|
||||
return signature(client_method).parameters[argument_name].default
|
||||
|
||||
raise TypeError("{}() takes no argument called '{}'".format(
|
||||
client_method.__name__, argument_name
|
||||
))
|
||||
|
||||
|
||||
def _make_key(key_format, client_method, args, kwargs):
|
||||
return key_format.format(**{
|
||||
argument_name: _get_argument(argument_name, client_method, args, kwargs)
|
||||
for argument_name in list(signature(client_method).parameters)
|
||||
})
|
||||
|
||||
|
||||
def set(key_format):
|
||||
|
||||
def _set(client_method):
|
||||
|
||||
@wraps(client_method)
|
||||
def new_client_method(client_instance, *args, **kwargs):
|
||||
redis_key = _make_key(key_format, client_method, args, kwargs)
|
||||
cached = redis_client.get(redis_key)
|
||||
if cached:
|
||||
return json.loads(cached.decode('utf-8'))
|
||||
api_response = client_method(client_instance, *args, **kwargs)
|
||||
redis_client.set(
|
||||
redis_key,
|
||||
json.dumps(api_response),
|
||||
ex=TTL,
|
||||
)
|
||||
return api_response
|
||||
|
||||
return new_client_method
|
||||
return _set
|
||||
|
||||
|
||||
def delete(key_format):
|
||||
|
||||
def _delete(client_method):
|
||||
|
||||
@wraps(client_method)
|
||||
def new_client_method(client_instance, *args, **kwargs):
|
||||
try:
|
||||
api_response = client_method(client_instance, *args, **kwargs)
|
||||
finally:
|
||||
redis_key = _make_key(key_format, client_method, args, kwargs)
|
||||
redis_client.delete(redis_key)
|
||||
return api_response
|
||||
|
||||
return new_client_method
|
||||
|
||||
return _delete
|
||||
@@ -23,5 +23,5 @@ notifications-python-client==5.6.0
|
||||
awscli-cwlogs>=1.4,<1.5
|
||||
itsdangerous==1.1.0
|
||||
|
||||
git+https://github.com/alphagov/notifications-utils.git@39.6.0#egg=notifications-utils==39.6.0
|
||||
git+https://github.com/alphagov/notifications-utils.git@39.7.0#egg=notifications-utils==39.7.0
|
||||
git+https://github.com/alphagov/govuk-frontend-jinja.git@v0.5.1-alpha#egg=govuk-frontend-jinja==0.5.1-alpha
|
||||
|
||||
@@ -25,16 +25,16 @@ notifications-python-client==5.6.0
|
||||
awscli-cwlogs>=1.4,<1.5
|
||||
itsdangerous==1.1.0
|
||||
|
||||
git+https://github.com/alphagov/notifications-utils.git@39.6.0#egg=notifications-utils==39.6.0
|
||||
git+https://github.com/alphagov/notifications-utils.git@39.7.0#egg=notifications-utils==39.7.0
|
||||
git+https://github.com/alphagov/govuk-frontend-jinja.git@v0.5.1-alpha#egg=govuk-frontend-jinja==0.5.1-alpha
|
||||
|
||||
## The following requirements were added by pip freeze:
|
||||
awscli==1.18.82
|
||||
awscli==1.18.84
|
||||
bleach==3.1.4
|
||||
boto3==1.10.38
|
||||
botocore==1.17.5
|
||||
botocore==1.17.7
|
||||
cachetools==4.1.0
|
||||
certifi==2020.4.5.2
|
||||
certifi==2020.6.20
|
||||
chardet==3.0.4
|
||||
click==7.1.2
|
||||
colorama==0.4.3
|
||||
|
||||
Reference in New Issue
Block a user