Cache serialised template in Redis

This commit is contained in:
Chris Hill-Scott
2020-06-22 09:39:47 +01:00
parent 2f4470edca
commit 996800f90d
2 changed files with 44 additions and 0 deletions

View File

@@ -4,11 +4,14 @@ from functools import partial
from threading import RLock from threading import RLock
import cachetools import cachetools
from notifications_utils.clients.redis import RequestCache
from app import redis_store
from app.dao import templates_dao from app.dao import templates_dao
caches = defaultdict(partial(cachetools.TTLCache, maxsize=1024, ttl=2)) caches = defaultdict(partial(cachetools.TTLCache, maxsize=1024, ttl=2))
locks = defaultdict(RLock) locks = defaultdict(RLock)
redis_cache = RequestCache(redis_store)
def memory_cache(func): def memory_cache(func):
@@ -69,6 +72,7 @@ class SerialisedTemplate(SerialisedModel):
return cls(cls.get_dict(template_id, service_id)) return cls(cls.get_dict(template_id, service_id))
@staticmethod @staticmethod
@redis_cache.set('template-{template_id}-version-None')
def get_dict(template_id, service_id): def get_dict(template_id, service_id):
from app.schemas import template_schema from app.schemas import template_schema

View File

@@ -239,6 +239,46 @@ def test_should_cache_template_lookups_in_memory(mocker, client, sample_template
assert Notification.query.count() == 5 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", @pytest.mark.parametrize("notification_type, key_send_to, send_to",
[("sms", "phone_number", "+447700900855"), [("sms", "phone_number", "+447700900855"),
("email", "email_address", "sample@email.com")]) ("email", "email_address", "sample@email.com")])