Skip to content

Commit 517be27

Browse files
committed
Remove conditional blocks for unsupported Django versions
1 parent 8554240 commit 517be27

File tree

6 files changed

+23
-41
lines changed

6 files changed

+23
-41
lines changed

django_prometheus/cache/backends/redis.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django import VERSION as DJANGO_VERSION
1+
from django.core.cache.backends.redis import RedisCache as DjangoRedisCache
22
from django_redis import cache, exceptions
33

44
from django_prometheus.cache.metrics import (
@@ -33,20 +33,18 @@ def get(self, key, default=None, version=None, client=None):
3333
return default
3434

3535

36-
if DJANGO_VERSION >= (4, 0):
37-
from django.core.cache.backends.redis import RedisCache as DjangoRedisCache
3836

39-
class NativeRedisCache(DjangoRedisCache):
40-
def get(self, key, default=None, version=None):
41-
django_cache_get_total.labels(backend="native_redis").inc()
42-
try:
43-
result = super().get(key, default=None, version=version)
44-
except Exception:
45-
django_cache_get_fail_total.labels(backend="native_redis").inc()
46-
raise
47-
if result is not None:
48-
django_cache_hits_total.labels(backend="native_redis").inc()
49-
return result
50-
else:
51-
django_cache_misses_total.labels(backend="native_redis").inc()
52-
return default
37+
class NativeRedisCache(DjangoRedisCache):
38+
def get(self, key, default=None, version=None):
39+
django_cache_get_total.labels(backend="native_redis").inc()
40+
try:
41+
result = super().get(key, default=None, version=version)
42+
except Exception:
43+
django_cache_get_fail_total.labels(backend="native_redis").inc()
44+
raise
45+
if result is not None:
46+
django_cache_hits_total.labels(backend="native_redis").inc()
47+
return result
48+
else:
49+
django_cache_misses_total.labels(backend="native_redis").inc()
50+
return default

django_prometheus/db/backends/common.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

django_prometheus/db/backends/postgis/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from django.contrib.gis.db.backends.postgis import base
2+
from django.db.backends.postgresql.base import Cursor
23

3-
from django_prometheus.db.backends.common import get_postgres_cursor_class
44
from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWrapper
55

66

77
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
88
def get_new_connection(self, *args, **kwargs):
99
conn = super().get_new_connection(*args, **kwargs)
1010
conn.cursor_factory = ExportingCursorWrapper(
11-
conn.cursor_factory or get_postgres_cursor_class(), "postgis", self.vendor
11+
conn.cursor_factory or Cursor(), "postgis", self.vendor
1212
)
1313
return conn
1414

django_prometheus/db/backends/postgresql/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from django.db.backends.postgresql import base
2+
from django.db.backends.postgresql.base import Cursor
23

3-
from django_prometheus.db.backends.common import get_postgres_cursor_class
44
from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWrapper
55

66

77
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
88
def get_new_connection(self, *args, **kwargs):
99
conn = super().get_new_connection(*args, **kwargs)
1010
conn.cursor_factory = ExportingCursorWrapper(
11-
conn.cursor_factory or get_postgres_cursor_class(), self.alias, self.vendor
11+
conn.cursor_factory or Cursor(), self.alias, self.vendor
1212
)
1313
return conn
1414

django_prometheus/tests/end2end/testapp/settings.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import os
22
import tempfile
33

4-
from django import VERSION as DJANGO_VERSION
5-
64
from testapp.helpers import get_middleware
75

86
# SECURITY WARNING: keep the secret key used in production secret!
@@ -135,13 +133,11 @@
135133
"LOCATION": "redis://127.0.0.1:6666/1",
136134
"OPTIONS": {"IGNORE_EXCEPTIONS": True},
137135
},
138-
}
139-
140-
if DJANGO_VERSION >= (4, 0):
141-
CACHES["native_redis"] = {
136+
"native_redis": {
142137
"BACKEND": "django_prometheus.cache.backends.redis.NativeRedisCache",
143138
"LOCATION": "redis://127.0.0.1:6379/0",
144-
}
139+
},
140+
}
145141

146142

147143
# Internationalization

django_prometheus/tests/end2end/testapp/test_caches.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import pytest
2-
from django import VERSION as DJANGO_VERSION
32
from django.core.cache import caches
43
from redis import RedisError
54

65
from django_prometheus.testutils import assert_metric_equal, get_metric
76

8-
_SUPPORTED_CACHES = ["memcached.PyLibMCCache", "memcached.PyMemcacheCache", "filebased", "locmem", "redis"]
9-
if DJANGO_VERSION >= (4, 0):
10-
_SUPPORTED_CACHES.append("native_redis")
7+
_SUPPORTED_CACHES = ["memcached.PyLibMCCache", "memcached.PyMemcacheCache", "filebased", "locmem", "redis", "native_redis"]
118

129

1310
class TestCachesMetrics:

0 commit comments

Comments
 (0)