From e96708873c7aaf49f5b571cb08804aaecb45cc9d Mon Sep 17 00:00:00 2001 From: Yaser Amiri Date: Tue, 12 Dec 2023 18:54:01 +0330 Subject: [PATCH] Support Django's official Redis cache backend. (#409) --- django_prometheus/cache/backends/redis.py | 22 ++++++++++++++++++- .../tests/end2end/testapp/settings.py | 8 +++++++ .../tests/end2end/testapp/test_caches.py | 3 +++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/django_prometheus/cache/backends/redis.py b/django_prometheus/cache/backends/redis.py index f8af1a7a..94e4d01f 100644 --- a/django_prometheus/cache/backends/redis.py +++ b/django_prometheus/cache/backends/redis.py @@ -1,5 +1,5 @@ +from django import VERSION as DJANGO_VERSION from django_redis import cache, exceptions - from django_prometheus.cache.metrics import ( django_cache_get_fail_total, django_cache_get_total, @@ -30,3 +30,23 @@ def get(self, key, default=None, version=None, client=None): else: django_cache_misses_total.labels(backend="redis").inc() return default + + +if DJANGO_VERSION >= (4, 0): + from django.core.cache.backends.redis import RedisCache as DjangoRedisCache + + class NativeRedisCache(DjangoRedisCache): + + def get(self, key, default=None, version=None): + django_cache_get_total.labels(backend="native_redis").inc() + try: + result = super().get(key, default=None, version=version) + except Exception: + django_cache_get_fail_total.labels(backend="native_redis").inc() + raise + if result is not None: + django_cache_hits_total.labels(backend="native_redis").inc() + return result + else: + django_cache_misses_total.labels(backend="native_redis").inc() + return default diff --git a/django_prometheus/tests/end2end/testapp/settings.py b/django_prometheus/tests/end2end/testapp/settings.py index dad2cb68..60254d8d 100644 --- a/django_prometheus/tests/end2end/testapp/settings.py +++ b/django_prometheus/tests/end2end/testapp/settings.py @@ -1,6 +1,8 @@ import os import tempfile +from django import VERSION as DJANGO_VERSION + from testapp.helpers import get_middleware # SECURITY WARNING: keep the secret key used in production secret! @@ -131,6 +133,12 @@ }, } +if DJANGO_VERSION >= (4, 0): + CACHES["native_redis"] = { + "BACKEND": "django_prometheus.cache.backends.redis.NativeRedisCache", + "LOCATION": "redis://127.0.0.1:6379/0", + } + # Internationalization LANGUAGE_CODE = "en-us" diff --git a/django_prometheus/tests/end2end/testapp/test_caches.py b/django_prometheus/tests/end2end/testapp/test_caches.py index 84ccbb6c..389dc5ae 100644 --- a/django_prometheus/tests/end2end/testapp/test_caches.py +++ b/django_prometheus/tests/end2end/testapp/test_caches.py @@ -1,10 +1,13 @@ import pytest from django.core.cache import caches +from django import VERSION as DJANGO_VERSION from redis import RedisError from django_prometheus.testutils import assert_metric_equal, get_metric _SUPPORTED_CACHES = ["memcached.PyLibMCCache", "memcached.PyMemcacheCache", "filebased", "locmem", "redis"] +if DJANGO_VERSION >= (4, 0): + _SUPPORTED_CACHES.append("native_redis") class TestCachesMetrics: