Skip to content

Commit

Permalink
Update service template cache keys when a template category is updated (
Browse files Browse the repository at this point in the history
#1899)

* Update service template cache keys when a template category is updated

* Rever unneeded changes
  • Loading branch information
whabanks authored Jul 22, 2024
1 parent 84a3c62 commit b66ce61
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions app/notify_client/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,46 @@ def _make_key(key_format, client_method, args, kwargs):
)


def set_service_template(key_format):
def _set(client_method):
@wraps(client_method)
def new_client_method(client_instance, *args, **kwargs):
"""
This decorator is functionaly the same as the `set` decorator. The only difference is that it checks if the
category of the service template is dirty and updates it if it is. When a category is updated via the admin
UI, the category is updated in the cache. However, the service template is not updated in the cache. This
decorator checks the category on the template against the cached category and updates the template if it is
dirty
"""
redis_key = _make_key(key_format, client_method, args, kwargs)
cached_template = redis_client.get(redis_key)

if cached_template:
template_category = json.loads(cached_template.decode("utf-8")).get("template_category")
cached_category = redis_client.get(f"template_category-{template_category['id']}") if template_category else None

if cached_category:
category = json.loads(cached_category.decode("utf-8"))

if not category == template_category:
redis_client.set(redis_key, json.dumps(cached_category), ex=TTL)

return json.loads(cached_template.decode("utf-8"))

api_response = client_method(client_instance, *args, **kwargs)

redis_client.set(
redis_key,
json.dumps(api_response),
ex=TTL,
)
return api_response

return new_client_method

return _set


def set(key_format):
def _set(client_method):
@wraps(client_method)
Expand Down
2 changes: 1 addition & 1 deletion app/notify_client/service_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def update_service_template_postage(self, service_id, template_id, postage):
_attach_current_user({"postage": postage}),
)

@cache.set("template-{template_id}-version-{version}")
@cache.set_service_template("template-{template_id}-version-{version}")
def get_service_template(self, service_id, template_id, version=None):
"""
Retrieve a service template.
Expand Down

0 comments on commit b66ce61

Please sign in to comment.