diff --git a/django_prometheus/cache/backends/redis.py b/django_prometheus/cache/backends/redis.py index bc982dac..29b6f044 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 warnings import deprecated +from django.core.cache.backends.redis import RedisCache from django_prometheus.cache.metrics import ( django_cache_get_fail_total, @@ -9,44 +9,19 @@ ) -class RedisCache(cache.RedisCache): - """Inherit redis to add metrics about hit/miss/interruption ratio""" - - @cache.omit_exception - def get(self, key, default=None, version=None, client=None): +class NativeRedisCache(RedisCache): + def get(self, key, default=None, version=None): + django_cache_get_total.labels(backend="native_redis").inc() try: - django_cache_get_total.labels(backend="redis").inc() - cached = self.client.get(key, default=None, version=version, client=client) - except exceptions.ConnectionInterrupted as e: - django_cache_get_fail_total.labels(backend="redis").inc() - if self._ignore_exceptions: - if self._log_ignored_exceptions: - cache.logger.error(str(e)) - return default + 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: - if cached is not None: - django_cache_hits_total.labels(backend="redis").inc() - return cached - 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 + django_cache_misses_total.labels(backend="native_redis").inc() + return default - 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 +RedisCache = deprecated("RedisCache is deprecated, use NativeRedisCache instead")(NativeRedisCache) diff --git a/django_prometheus/tests/end2end/testapp/settings.py b/django_prometheus/tests/end2end/testapp/settings.py index abad986f..e7eb76f6 100644 --- a/django_prometheus/tests/end2end/testapp/settings.py +++ b/django_prometheus/tests/end2end/testapp/settings.py @@ -120,6 +120,10 @@ "BACKEND": "django_prometheus.cache.backends.locmem.LocMemCache", "LOCATION": os.path.join(_tmp_cache_dir, "locmem_cache"), }, + "native_redis": { + "BACKEND": "django_prometheus.cache.backends.redis.NativeRedisCache", + "LOCATION": "redis://127.0.0.1:6379/0", + }, "redis": { "BACKEND": "django_prometheus.cache.backends.redis.RedisCache", "LOCATION": "redis://127.0.0.1:6379/1", @@ -136,12 +140,6 @@ }, } -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 713fd2fb..ed1ffcd6 100644 --- a/django_prometheus/tests/end2end/testapp/test_caches.py +++ b/django_prometheus/tests/end2end/testapp/test_caches.py @@ -1,13 +1,17 @@ import pytest -from django import VERSION as DJANGO_VERSION from django.core.cache import caches 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") +_SUPPORTED_CACHES = [ + "memcached.PyLibMCCache", + "memcached.PyMemcacheCache", + "filebased", + "locmem", + "native_redis", + "redis", +] class TestCachesMetrics: diff --git a/requirements.txt b/requirements.txt index 50787ea8..f64870ca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -django-redis>=4.12.1 black flake8 prometheus-client>=0.12.0 @@ -10,5 +9,6 @@ pytest-django pylibmc pymemcache python-memcached +redis setuptools<72.0.0 -wheel +wheel \ No newline at end of file