Cache redis version in the connection object (#1742)

This commit is contained in:
Oscar Arbeláez-Echeverri 2022-12-28 18:58:13 -05:00 committed by GitHub
parent 50d8d72928
commit 9aaceb22e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -303,7 +303,14 @@ def get_version(connection: 'Redis'):
connection (Redis): The Redis connection. connection (Redis): The Redis connection.
""" """
try: try:
return tuple(int(i) for i in connection.info("server")["redis_version"].split('.')[:3]) # Getting the connection info for each job tanks performance, we can cache it on the connection object
if not getattr(connection, "__rq_redis_server_version", None):
setattr(
connection,
"__rq_redis_server_version",
tuple(int(i) for i in connection.info("server")["redis_version"].split('.')[:3])
)
return getattr(connection, "__rq_redis_server_version")
except ResponseError: # fakeredis doesn't implement Redis' INFO command except ResponseError: # fakeredis doesn't implement Redis' INFO command
return (5, 0, 9) return (5, 0, 9)

View File

@ -1,5 +1,6 @@
import re import re
import datetime import datetime
from unittest.mock import Mock
from redis import Redis from redis import Redis
@ -85,6 +86,15 @@ class TestUtils(RQTestCase):
return {'redis_version': '3.0.7.9'} return {'redis_version': '3.0.7.9'}
self.assertEqual(get_version(DummyRedis()), (3, 0, 7)) self.assertEqual(get_version(DummyRedis()), (3, 0, 7))
def test_get_redis_version_gets_cached(self):
"""Ensure get_version works properly"""
# Parses 3 digit version numbers correctly
redis = Mock(spec=['info'])
redis.info = Mock(return_value={'redis_version': '4.0.8'})
self.assertEqual(get_version(redis), (4, 0, 8))
self.assertEqual(get_version(redis), (4, 0, 8))
redis.info.assert_called_once()
def test_ceildiv_even(self): def test_ceildiv_even(self):
"""When a number is evenly divisible by another ceildiv returns the quotient""" """When a number is evenly divisible by another ceildiv returns the quotient"""
dividend = 12 dividend = 12