Skip to content

Drop django-redis #454

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
44 changes: 16 additions & 28 deletions django_prometheus/cache/backends/redis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import warnings

from django.core.cache.backends.redis import RedisCache as DjangoRedisCache
from django_redis import cache, exceptions

from django_prometheus.cache.metrics import (
django_cache_get_fail_total,
Expand All @@ -9,39 +10,26 @@
)


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):
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:
self.logger.error(str(e))
return default
raise
else:
if cached is not None:
django_cache_hits_total.labels(backend="redis").inc()
return cached
django_cache_misses_total.labels(backend="redis").inc()
return default


class NativeRedisCache(DjangoRedisCache):
class RedisCache(DjangoRedisCache):
def get(self, key, default=None, version=None):
django_cache_get_total.labels(backend="native_redis").inc()
django_cache_get_total.labels(backend="redis").inc()
try:
result = super().get(key, default=None, version=version)
except Exception:
django_cache_get_fail_total.labels(backend="native_redis").inc()
django_cache_get_fail_total.labels(backend="redis").inc()
raise
if result is not None:
django_cache_hits_total.labels(backend="native_redis").inc()
django_cache_hits_total.labels(backend="redis").inc()
return result
django_cache_misses_total.labels(backend="native_redis").inc()
return default


class NativeRedisCache(RedisCache):
def __init__(self, *args, **kwargs):
warnings.warn(
"NativeRedisCache is renamed, use RedisCache instead",
DeprecationWarning,
stacklevel=2,
)
super().__init__(*args, **kwargs)
4 changes: 0 additions & 4 deletions django_prometheus/tests/end2end/testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@
"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 Down
1 change: 0 additions & 1 deletion django_prometheus/tests/end2end/testapp/test_caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"memcached.PyMemcacheCache",
"filebased",
"locmem",
"native_redis",
"redis",
]

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
django-redis>=4.12.1
prometheus-client>=0.12.0
pip-prometheus>=1.2.1
mysqlclient
Expand All @@ -9,4 +8,5 @@ pylibmc
pymemcache
python-memcached
setuptools<72.0.0
redis
wheel
Loading