diff --git a/redis/connection.py b/redis/connection.py index 40f2d29722..4f8b82b71f 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -870,9 +870,11 @@ def read_response( and self._cache.get(self._current_command_cache_key).status != CacheEntryStatus.IN_PROGRESS ): - return copy.deepcopy( + res = copy.deepcopy( self._cache.get(self._current_command_cache_key).cache_value ) + self._current_command_cache_key = None + return res response = self._conn.read_response( disable_decoding=disable_decoding, @@ -898,6 +900,8 @@ def read_response( cache_entry.cache_value = response self._cache.set(cache_entry) + self._current_command_cache_key = None + return response def pack_command(self, *args): diff --git a/tests/test_connection.py b/tests/test_connection.py index a58703e3b5..2a5710963b 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -468,24 +468,6 @@ def test_read_response_returns_cached_reply(self, mock_cache, mock_connection): status=CacheEntryStatus.IN_PROGRESS, connection_ref=mock_connection, ), - CacheEntry( - cache_key=CacheKey(command="GET", redis_keys=("foo",)), - cache_value=b"bar", - status=CacheEntryStatus.VALID, - connection_ref=mock_connection, - ), - CacheEntry( - cache_key=CacheKey(command="GET", redis_keys=("foo",)), - cache_value=b"bar", - status=CacheEntryStatus.VALID, - connection_ref=mock_connection, - ), - CacheEntry( - cache_key=CacheKey(command="GET", redis_keys=("foo",)), - cache_value=b"bar", - status=CacheEntryStatus.VALID, - connection_ref=mock_connection, - ), ] mock_connection.send_command.return_value = Any mock_connection.read_response.return_value = b"bar" @@ -496,9 +478,9 @@ def test_read_response_returns_cached_reply(self, mock_cache, mock_connection): ) proxy_connection.send_command(*["GET", "foo"], **{"keys": ["foo"]}) assert proxy_connection.read_response() == b"bar" + assert proxy_connection._current_command_cache_key == None assert proxy_connection.read_response() == b"bar" - mock_connection.read_response.assert_called_once() mock_cache.set.assert_has_calls( [ call( @@ -525,9 +507,6 @@ def test_read_response_returns_cached_reply(self, mock_cache, mock_connection): call(CacheKey(command="GET", redis_keys=("foo",))), call(CacheKey(command="GET", redis_keys=("foo",))), call(CacheKey(command="GET", redis_keys=("foo",))), - call(CacheKey(command="GET", redis_keys=("foo",))), - call(CacheKey(command="GET", redis_keys=("foo",))), - call(CacheKey(command="GET", redis_keys=("foo",))), ] )