Cache service data retention in Redis

Adds caching for service data retention. This removes separate API
client methods to retrieve individual data retention records by id
or type in favor of a single method that fetches and caches all
retention settings configured for the service. This makes it much
easier to invalidate cache when settings change.

Lookup by id or type is provided by helper methods in the service
model.
This commit is contained in:
Alexey Bezhan
2018-12-03 17:19:38 +00:00
parent 8aeea9929f
commit 7a7a9ae854
9 changed files with 62 additions and 72 deletions

View File

@@ -226,8 +226,8 @@ def get_notifications(service_id, message_type, status_override=None):
service_data_retention_days = None
if message_type is not None:
service_data_retention_days = service_api_client.get_service_data_retention_by_notification_type(
service_id, message_type
service_data_retention_days = current_service.get_data_retention_by_type(
message_type
).get('days_of_retention', current_app.config['ACTIVITY_STATS_LIMIT_DAYS'])
if request.path.endswith('csv') and current_user.has_permissions('view_activity'):
@@ -406,8 +406,8 @@ def get_job_partials(job, template):
counts=_get_job_counts(job),
status=filter_args['status']
)
service_data_retention_days = service_api_client.get_service_data_retention_by_notification_type(
current_service.id, template['template_type']
service_data_retention_days = current_service.get_data_retention_by_type(
template['template_type']
).get('days_of_retention', current_app.config['ACTIVITY_STATS_LIMIT_DAYS'])
return {

View File

@@ -30,7 +30,6 @@ from app import (
format_date_numeric,
job_api_client,
notification_api_client,
service_api_client,
)
from app.main import main
from app.notify_client.api_key_api_client import KEY_TYPE_TEST
@@ -198,8 +197,8 @@ def download_notifications_csv(service_id):
filter_args = parse_filter_args(request.args)
filter_args['status'] = set_status_filters(filter_args)
service_data_retention_days = service_api_client.get_service_data_retention_by_notification_type(
service_id, filter_args.get('message_type')[0]
service_data_retention_days = current_service.get_data_retention_by_type(
filter_args.get('message_type')[0]
).get('days_of_retention', current_app.config['ACTIVITY_STATS_LIMIT_DAYS'])
return Response(
stream_with_context(

View File

@@ -268,7 +268,16 @@ class Service():
return service_api_client.get_service_data_retention(self.id)
def get_data_retention_item(self, id):
return service_api_client.get_service_data_retention_by_id(self.id, id)
return next(
(dr for dr in self.data_retention if dr['id'] == id),
None
)
def get_data_retention_by_type(self, notification_type):
return next(
(dr for dr in self.data_retention if dr['notification_type'] == notification_type),
{}
)
@property
def email_branding_id(self):

View File

@@ -488,6 +488,7 @@ class ServiceAPIClient(NotifyAdminAPIClient):
}
return self.post("/service/{}/delivery-receipt-api".format(service_id), data)
@cache.delete('service-{service_id}-data-retention')
def create_service_data_retention(self, service_id, notification_type, days_of_retention):
data = {
"notification_type": notification_type,
@@ -496,20 +497,16 @@ class ServiceAPIClient(NotifyAdminAPIClient):
return self.post("/service/{}/data-retention".format(service_id), data)
@cache.delete('service-{service_id}-data-retention')
def update_service_data_retention(self, service_id, data_retention_id, days_of_retention):
data = {
"days_of_retention": days_of_retention
}
return self.post("/service/{}/data-retention/{}".format(service_id, data_retention_id), data)
@cache.set('service-{service_id}-data-retention')
def get_service_data_retention(self, service_id):
return self.get("/service/{}/data-retention".format(service_id))
def get_service_data_retention_by_notification_type(self, service_id, notification_type):
return self.get("/service/{}/data-retention/notification-type/{}".format(service_id, notification_type))
def get_service_data_retention_by_id(self, service_id, data_retention_id):
return self.get("service/{}/data-retention/{}".format(service_id, data_retention_id))
service_api_client = ServiceAPIClient()