From 5335729c3145f70934f53b85bb57065c4a1c0940 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 22 Jun 2020 09:39:32 +0100 Subject: [PATCH] Import request cache from utils MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- app/notify_client/__init__.py | 5 +++ app/notify_client/cache.py | 73 ----------------------------------- requirements-app.txt | 2 +- requirements.txt | 8 ++-- 4 files changed, 10 insertions(+), 78 deletions(-) delete mode 100644 app/notify_client/cache.py diff --git a/app/notify_client/__init__.py b/app/notify_client/__init__.py index fa97a4c6b..3145ebc44 100644 --- a/app/notify_client/__init__.py +++ b/app/notify_client/__init__.py @@ -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): diff --git a/app/notify_client/cache.py b/app/notify_client/cache.py deleted file mode 100644 index bc8d9d865..000000000 --- a/app/notify_client/cache.py +++ /dev/null @@ -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 diff --git a/requirements-app.txt b/requirements-app.txt index 12163b5e0..a26c2a81a 100644 --- a/requirements-app.txt +++ b/requirements-app.txt @@ -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 diff --git a/requirements.txt b/requirements.txt index c0dbe63e3..e56517437 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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