From 57fb9da4140f84275982a897b587e22ee61346fd Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Wed, 23 Jun 2021 15:09:09 +0100 Subject: [PATCH] - change the condition so that we don't reset the cache if it's zero - set the cache if it doesn't exist so there is an expiry of 24 hours. --- app/notifications/process_notifications.py | 11 +++++++++-- app/notifications/validators.py | 2 +- tests/app/notifications/test_process_notification.py | 2 ++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index b6cdc26fe..4aea1ec05 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -149,8 +149,15 @@ def persist_notification( if not simulated: dao_create_notification(notification) if key_type != KEY_TYPE_TEST and current_app.config['REDIS_ENABLED']: - redis_store.incr(redis.daily_limit_cache_key(service.id)) - + cache_key = redis.daily_limit_cache_key(service.id) + if redis_store.get(cache_key) is None: + # if cache does not exist set the cache to 1 with an expiry of 24 hours, + # The cache should be set by the time we create the notification + # but in case it is this will make sure the expiry is set to 24 hours, + # where if we let the incr method create the cache it will be set a ttl. + redis_store.set(cache_key, 1, ex=86400) + else: + redis_store.incr(cache_key) current_app.logger.info( "{} {} created at {}".format(notification_type, notification_id, notification_created_at) ) diff --git a/app/notifications/validators.py b/app/notifications/validators.py index b33bc79a8..60bf639e1 100644 --- a/app/notifications/validators.py +++ b/app/notifications/validators.py @@ -61,7 +61,7 @@ def check_service_over_daily_message_limit(key_type, service): if key_type != KEY_TYPE_TEST and current_app.config['REDIS_ENABLED']: cache_key = daily_limit_cache_key(service.id) service_stats = redis_store.get(cache_key) - if not service_stats: + if service_stats is None: # first message of the day, set the cache to 0 and the expiry to 24 hours service_stats = 0 redis_store.set(cache_key, service_stats, ex=86400) diff --git a/tests/app/notifications/test_process_notification.py b/tests/app/notifications/test_process_notification.py index cb9703fb0..051e2953b 100644 --- a/tests/app/notifications/test_process_notification.py +++ b/tests/app/notifications/test_process_notification.py @@ -202,6 +202,7 @@ def test_persist_notification_increments_cache_for_trial_service( service = create_service(restricted=True) template = create_template(service=service) api_key = create_api_key(service=service) + mocker.patch('app.notifications.process_notifications.redis_store.get', return_value=1) mock_incr = mocker.patch('app.notifications.process_notifications.redis_store.incr') with set_config(notify_api, 'REDIS_ENABLED', True): persist_notification( @@ -224,6 +225,7 @@ def test_persist_notification_increments_cache_live_service( service = create_service(restricted=False) template = create_template(service=service) api_key = create_api_key(service=service) + mocker.patch('app.notifications.process_notifications.redis_store.get', return_value=1) mock_incr = mocker.patch('app.notifications.process_notifications.redis_store.incr') with set_config(notify_api, 'REDIS_ENABLED', True): persist_notification(