Fixed #36879 -- Identified Django client in Redis client metadata.

This commit is contained in:
ar3ph
2026-02-02 13:26:18 -05:00
committed by Jacob Walls
parent 24a14860ce
commit 72ae25a9c2
3 changed files with 34 additions and 1 deletions

View File

@@ -113,6 +113,7 @@ answer newbie questions, and generally made Django that much better:
Anubhav Joshi <anubhav9042@gmail.com>
Anvesh Mishra <anveshgreat11@gmail.com>
Anže Pečar <anze@pecar.me>
ar3ph <https://github.com/ar3ph>
A. Rafey Khan <khanxbahria@gmail.com>
Aram Dulyan
arien <regexbot@gmail.com>

View File

@@ -4,6 +4,7 @@ import pickle
import random
import re
import django
from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache
from django.utils.functional import cached_property
from django.utils.module_loading import import_string
@@ -59,7 +60,19 @@ class RedisCacheClient:
parser_class = import_string(parser_class)
parser_class = parser_class or self._lib.connection.DefaultParser
self._pool_options = {"parser_class": parser_class, **options}
version = django.get_version()
if hasattr(self._lib, "DriverInfo"):
driver_info = self._lib.DriverInfo().add_upstream_driver("django", version)
driver_info_options = {"driver_info": driver_info}
else:
# DriverInfo is not available in this redis-py version.
driver_info_options = {"lib_name": f"redis-py(django_v{version})"}
self._pool_options = {
"parser_class": parser_class,
**driver_info_options,
**options,
}
def _get_connection_pool_index(self, write):
# Write to the first server. Read from other servers if there are more,

19
tests/cache/tests.py vendored
View File

@@ -15,6 +15,7 @@ from functools import wraps
from pathlib import Path
from unittest import mock, skipIf
import django
from django.conf import settings
from django.core import management, signals
from django.core.cache import (
@@ -1986,6 +1987,24 @@ class RedisCacheTests(BaseCacheTests, TestCase):
self.assertEqual(pool.connection_kwargs["socket_timeout"], 0.1)
self.assertIs(pool.connection_kwargs["retry_on_timeout"], True)
def test_client_driver_info(self):
client_info = cache._cache.get_client().client_info()
if {"lib-name", "lib-ver"}.issubset(client_info):
version = django.get_version()
if hasattr(self.lib, "DriverInfo"):
info = self._lib.DriverInfo().add_upstream_driver("django", version)
correct_lib_name = info.formatted_name
else:
correct_lib_name = f"redis-py(django_v{version})"
# Relax the assertion to allow date variance in editable installs.
truncated_lib_name = correct_lib_name.rsplit(".dev", maxsplit=1)[0]
self.assertIn(truncated_lib_name, client_info["lib-name"])
self.assertEqual(client_info["lib-ver"], self.lib.__version__)
else:
# Redis versions below 7.2 lack CLIENT SETINFO.
self.assertNotIn("lib-ver", client_info)
self.assertNotIn("lib-name", client_info)
class FileBasedCachePathLibTests(FileBasedCacheTests):
def mkdtemp(self):