diff --git a/app/serialised_models.py b/app/serialised_models.py index 1896c1810..019487b0c 100644 --- a/app/serialised_models.py +++ b/app/serialised_models.py @@ -4,11 +4,14 @@ from functools import partial from threading import RLock import cachetools +from notifications_utils.clients.redis import RequestCache +from app import redis_store from app.dao import templates_dao caches = defaultdict(partial(cachetools.TTLCache, maxsize=1024, ttl=2)) locks = defaultdict(RLock) +redis_cache = RequestCache(redis_store) def memory_cache(func): @@ -69,6 +72,7 @@ class SerialisedTemplate(SerialisedModel): return cls(cls.get_dict(template_id, service_id)) @staticmethod + @redis_cache.set('template-{template_id}-version-None') def get_dict(template_id, service_id): from app.schemas import template_schema diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index 4348ae24d..3632e45a0 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -239,6 +239,46 @@ def test_should_cache_template_lookups_in_memory(mocker, client, sample_template assert Notification.query.count() == 5 +def test_should_cache_template_lookups_in_redis(mocker, client, sample_template): + + from app.schemas import template_schema + + mock_redis_get = mocker.patch( + 'app.redis_store.get', + return_value=None, + ) + mock_redis_set = mocker.patch( + 'app.redis_store.set', + ) + + mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') + + data = { + 'phone_number': '+447700900855', + 'template_id': str(sample_template.id), + } + + auth_header = create_authorization_header(service_id=sample_template.service_id) + client.post( + path='/v2/notifications/sms', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header] + ) + + expected_key = f'template-{sample_template.id}-version-None' + + assert mock_redis_get.call_args_list == [call( + expected_key, + )] + + template_dict = template_schema.dump(sample_template).data + + assert len(mock_redis_set.call_args_list) == 1 + assert mock_redis_set.call_args[0][0] == expected_key + assert json.loads(mock_redis_set.call_args[0][1]) == template_dict + assert mock_redis_set.call_args[1]['ex'] == 604_800 + + @pytest.mark.parametrize("notification_type, key_send_to, send_to", [("sms", "phone_number", "+447700900855"), ("email", "email_address", "sample@email.com")])