Change code order for Redis delete decorator

Before, the delete decorator would delete the keys from Redis and then
we made the request to api to change the data. However, it is possible
that the cache could get re-populated in between these two things
happening, and so would cache outdated data.

This changes the order to send the api request first. We then always
delete the specified keys from Redis. Changing the order of the code in
the decorator changes the order in which the cache keys get deleted, so
the tests have been updated.
This commit is contained in:
Katie Smith
2019-07-26 16:19:03 +01:00
parent ccad068a48
commit 123b769771
7 changed files with 26 additions and 23 deletions

View File

@@ -61,9 +61,12 @@ def delete(key_format):
@wraps(client_method)
def new_client_method(client_instance, *args, **kwargs):
redis_key = _make_key(key_format, client_method, args, kwargs)
redis_client.delete(redis_key)
return client_method(client_instance, *args, **kwargs)
try:
api_response = client_method(client_instance, *args, **kwargs)
finally:
redis_key = _make_key(key_format, client_method, args, kwargs)
redis_client.delete(redis_key)
return api_response
return new_client_method