Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drop django-redis #449

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 14 additions & 39 deletions django_prometheus/cache/backends/redis.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
10 changes: 4 additions & 6 deletions django_prometheus/tests/end2end/testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
12 changes: 8 additions & 4 deletions django_prometheus/tests/end2end/testapp/test_caches.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
django-redis>=4.12.1
black
flake8
prometheus-client>=0.12.0
Expand All @@ -10,5 +9,6 @@ pytest-django
pylibmc
pymemcache
python-memcached
redis
setuptools<72.0.0
wheel
wheel
Loading