- 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.
This commit is contained in:
Rebecca Law
2021-06-23 15:09:09 +01:00
parent 35b20ba363
commit 57fb9da414
3 changed files with 12 additions and 3 deletions

View File

@@ -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)
)

View File

@@ -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)