Add caching of templates in Redis

A lot of the frequently-used pages in the admin app rely on the API to
get templates.

So this commit adds three new caches:
- a single template version (including a key without a version number,
  which is the current version)
- all the templates for a service
- all versions of a template

The first will be the most crucial for performance, but there’s not much
cost to adding the other two.
This commit is contained in:
Chris Hill-Scott
2018-04-19 10:24:35 +01:00
parent 6101e5da43
commit cea7a027e3
3 changed files with 137 additions and 4 deletions

View File

@@ -6,10 +6,12 @@ from tests.conftest import SERVICE_ONE_ID, fake_uuid
from app import invite_api_client, service_api_client, user_api_client
from app.notify_client.service_api_client import ServiceAPIClient
FAKE_TEMPLATE_ID = fake_uuid()
def test_client_posts_archived_true_when_deleting_template(mocker):
service_id = fake_uuid
template_id = fake_uuid
service_id = fake_uuid()
template_id = fake_uuid()
mocker.patch('app.notify_client.current_user', id='1')
expected_data = {
@@ -139,6 +141,8 @@ def test_client_returns_count_of_service_templates(
@pytest.mark.parametrize(
(
'client_method,'
'extra_args,'
'expected_cache_get_calls,'
'cache_value,'
'expected_api_calls,'
@@ -147,6 +151,8 @@ def test_client_returns_count_of_service_templates(
),
[
(
service_api_client.get_service,
[SERVICE_ONE_ID],
[
call('service-{}'.format(SERVICE_ONE_ID))
],
@@ -156,6 +162,8 @@ def test_client_returns_count_of_service_templates(
{'data_from': 'cache'},
),
(
service_api_client.get_service,
[SERVICE_ONE_ID],
[
call('service-{}'.format(SERVICE_ONE_ID))
],
@@ -172,10 +180,72 @@ def test_client_returns_count_of_service_templates(
],
{'data_from': 'api'},
),
(
service_api_client.get_service_template,
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID],
[
call('template-{}'.format(FAKE_TEMPLATE_ID))
],
b'{"data_from": "cache"}',
[],
[],
{'data_from': 'cache'},
),
(
service_api_client.get_service_template,
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID],
[
call('template-{}'.format(FAKE_TEMPLATE_ID))
],
None,
[
call('/service/{}/template/{}'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID))
],
[
call(
'template-{}'.format(FAKE_TEMPLATE_ID),
'{"data_from": "api"}',
ex=86400
)
],
{'data_from': 'api'},
),
(
service_api_client.get_service_template,
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID, 1],
[
call('template-{}-1'.format(FAKE_TEMPLATE_ID))
],
b'{"data_from": "cache"}',
[],
[],
{'data_from': 'cache'},
),
(
service_api_client.get_service_template,
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID, 1],
[
call('template-{}-1'.format(FAKE_TEMPLATE_ID))
],
None,
[
call('/service/{}/template/{}/version/1'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID))
],
[
call(
'template-{}-1'.format(FAKE_TEMPLATE_ID),
'{"data_from": "api"}',
ex=86400
)
],
{'data_from': 'api'},
),
]
)
def test_returns_value_from_cache(
mocker,
client_method,
extra_args,
expected_cache_get_calls,
cache_value,
expected_return_value,
@@ -195,7 +265,7 @@ def test_returns_value_from_cache(
'app.notify_client.RedisClient.set',
)
assert service_api_client.get_service(SERVICE_ONE_ID) == expected_return_value
assert client_method(*extra_args) == expected_return_value
assert mock_redis_get.call_args_list == expected_cache_get_calls
assert mock_api_get.call_args_list == expected_api_calls
@@ -240,3 +310,46 @@ def test_deletes_service_cache(
assert call('service-{}'.format(SERVICE_ONE_ID)) in mock_redis_delete.call_args_list
assert len(mock_request.call_args_list) == 1
@pytest.mark.parametrize('method, extra_args, expected_cache_deletes', [
('create_service_template', ['name', 'type_', 'content', SERVICE_ONE_ID], [
'templates-{}'.format(SERVICE_ONE_ID),
]),
('update_service_template', [FAKE_TEMPLATE_ID, 'foo', 'sms', 'bar', SERVICE_ONE_ID], [
'templates-{}'.format(SERVICE_ONE_ID),
'template-{}'.format(FAKE_TEMPLATE_ID),
'template_versions-{}'.format(FAKE_TEMPLATE_ID),
]),
('redact_service_template', [SERVICE_ONE_ID, FAKE_TEMPLATE_ID], [
'templates-{}'.format(SERVICE_ONE_ID),
'template-{}'.format(FAKE_TEMPLATE_ID),
'template_versions-{}'.format(FAKE_TEMPLATE_ID),
]),
('update_service_template_sender', [SERVICE_ONE_ID, FAKE_TEMPLATE_ID, 'foo'], [
'templates-{}'.format(SERVICE_ONE_ID),
'template-{}'.format(FAKE_TEMPLATE_ID),
'template_versions-{}'.format(FAKE_TEMPLATE_ID),
]),
('delete_service_template', [SERVICE_ONE_ID, FAKE_TEMPLATE_ID], [
'templates-{}'.format(SERVICE_ONE_ID),
'template-{}'.format(FAKE_TEMPLATE_ID),
'template_versions-{}'.format(FAKE_TEMPLATE_ID),
]),
])
def test_deletes_caches_when_modifying_templates(
app_,
mock_get_user,
mocker,
method,
extra_args,
expected_cache_deletes,
):
mocker.patch('app.notify_client.current_user', id='1')
mock_redis_delete = mocker.patch('app.notify_client.RedisClient.delete')
mock_request = mocker.patch('notifications_python_client.base.BaseAPIClient.request')
getattr(service_api_client, method)(*extra_args)
assert mock_redis_delete.call_args_list == list(map(call, expected_cache_deletes))
assert len(mock_request.call_args_list) == 1