From 5aa7b5a337fd536d00d84b1f2d0cb681cf8c120c Mon Sep 17 00:00:00 2001 From: sadnub Date: Sun, 10 Apr 2022 22:42:10 -0400 Subject: [PATCH] add custom cache backend for deleting many keys with a pattern --- api/tacticalrmm/tacticalrmm/cache.py | 10 ++++++++++ api/tacticalrmm/tacticalrmm/settings.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 api/tacticalrmm/tacticalrmm/cache.py diff --git a/api/tacticalrmm/tacticalrmm/cache.py b/api/tacticalrmm/tacticalrmm/cache.py new file mode 100644 index 00000000..fd3d5ef1 --- /dev/null +++ b/api/tacticalrmm/tacticalrmm/cache.py @@ -0,0 +1,10 @@ +from django.core.cache.backends.redis import RedisCache +from typing import Optional + + +class TacticalRedisCache(RedisCache): + def delete_many_pattern(self, pattern: str, version: Optional[int] = None) -> None: + keys = self._cache.get_client().keys(f":{version if version else 1}:{pattern}") + + if keys: + self._cache.delete_many(keys) diff --git a/api/tacticalrmm/tacticalrmm/settings.py b/api/tacticalrmm/tacticalrmm/settings.py index 839e8322..0d849634 100644 --- a/api/tacticalrmm/tacticalrmm/settings.py +++ b/api/tacticalrmm/tacticalrmm/settings.py @@ -151,7 +151,7 @@ CHANNEL_LAYERS = { CACHES = { "default": { - "BACKEND": "django.core.cache.backends.redis.RedisCache", + "BACKEND": "tacticalrmm.cache.TacticalRedisCache", "LOCATION": f"redis://{REDIS_HOST}:6379", "OPTIONS": { "parser_class": "redis.connection.HiredisParser",