Import request cache from utils

We’ve making the request cache shared code now so we can remove it from
this app and refer to the version in utils instead.

Delete unused module
This commit is contained in:
Chris Hill-Scott
2020-06-22 09:39:32 +01:00
parent 2f1e8b104b
commit 5335729c31
4 changed files with 10 additions and 78 deletions

View File

@@ -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):

View File

@@ -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` doesnt 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

View File

@@ -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

View File

@@ -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