Skip to content
Merged
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
15 changes: 14 additions & 1 deletion django/core/cache/backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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
Expand Down Expand Up @@ -59,7 +60,19 @@ def __init__(
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,
Expand Down
19 changes: 19 additions & 0 deletions tests/cache/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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 (
Expand Down Expand Up @@ -1986,6 +1987,24 @@ def test_redis_pool_options(self):
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):
Expand Down