Skip to content

Commit 58f7a6a

Browse files
FlauschbaellchenMarkus Richter
andauthored
CacheBackend: Use a configurable setting to specify cache key (#336)
The internal cache key used to write to and read from the cache backend could conflict with the main application. By introducing a Django setting it allows the developer to specify one different from the default value. Co-authored-by: Markus Richter <markus.richter@scale.eu>
1 parent 35b41aa commit 58f7a6a

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ on django.conf.settings with the required format to connect to your redis server
111111
REDIS_URL = redis://localhost:6370
112112
```
113113

114+
The cache healthcheck tries to write and read a specific key within the cache backend.
115+
It can be customized by setting `HEALTHCHECK_CACHE_KEY` to another value:
116+
117+
```python
118+
HEALTHCHECK_CACHE_KEY = custom_healthcheck_key
119+
```
120+
114121
## Setting up monitoring
115122

116123
You can use tools like Pingdom, StatusCake or other uptime robots to monitor service status.

docs/contrib.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ all the time.
5656

5757
You may also use both of them. To use these checks add them to `INSTALLED_APPS` in your
5858
Django settings module.
59+
60+
``cache``
61+
---------
62+
63+
The key `djangohealtcheck_test` will be written to the cache backend to validate that the cache is working.
64+
The name of the key can be customized by setting `HEALTHCHECK_CACHE_KEY` to another value.

docs/settings.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ exceeds 90% or available memory drops below 100 MB.
7171
memory falls below the specified value, a warning will be reported.
7272

7373
Celery Health Check
74-
----------------------
74+
-------------------
7575
Using `django.settings` you may exert more fine-grained control over the behavior of the celery health check
7676

7777
.. list-table:: Additional Settings
@@ -82,6 +82,10 @@ Using `django.settings` you may exert more fine-grained control over the behavio
8282
- Type
8383
- Default
8484
- Description
85+
* - `HEALTHCHECK_CACHE_KEY`
86+
- String
87+
- djangohealtcheck_test
88+
- Specifies the name of the key to write to and read from to validate that the cache is working.
8589
* - `HEALTHCHECK_CELERY_QUEUE_TIMEOUT`
8690
- Number
8791
- 3

health_check/cache/backends.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from django.core.cache import CacheKeyWarning, caches
2+
from django.conf import settings
23

34
from health_check.backends import BaseHealthCheckBackend
45
from health_check.exceptions import ServiceReturnedUnexpectedResult, ServiceUnavailable
56

6-
77
class CacheBackend(BaseHealthCheckBackend):
8+
89
def __init__(self, backend="default"):
910
super().__init__()
1011
self.backend = backend
12+
self.cache_key = getattr(settings, "HEALTHCHECK_CACHE_KEY", "djangohealtcheck_test")
1113

1214
def identifier(self):
1315
return f"Cache backend: {self.backend}"
@@ -16,9 +18,9 @@ def check_status(self):
1618
cache = caches[self.backend]
1719

1820
try:
19-
cache.set("djangohealtcheck_test", "itworks")
20-
if not cache.get("djangohealtcheck_test") == "itworks":
21-
raise ServiceUnavailable("Cache key does not match")
21+
cache.set(self.cache_key, "itworks")
22+
if not cache.get(self.cache_key) == "itworks":
23+
raise ServiceUnavailable(f"Cache key {self.cache_key} does not match")
2224
except CacheKeyWarning as e:
2325
self.add_error(ServiceReturnedUnexpectedResult("Cache key warning"), e)
2426
except ValueError as e:

0 commit comments

Comments
 (0)