Skip to content

Commit f62edaf

Browse files
committed
fix entry overwrite and reduce cache get set
1 parent 9772b5c commit f62edaf

File tree

2 files changed

+9
-30
lines changed

2 files changed

+9
-30
lines changed

redis/connection.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ def send_command(self, *args, **kwargs):
14751475
with self._pool_lock:
14761476
while cache_entry.connection_ref.can_read():
14771477
cache_entry.connection_ref.read_response(push_request=True)
1478-
# Check if entry still exists.
1478+
# Check if entry still exists, if so it must be cache_entry we got because of cache_lock
14791479
if self._cache.get(cache_key) is not None:
14801480
self._current_command_cache_entry = cache_entry
14811481
return
@@ -1533,17 +1533,12 @@ def read_response(
15331533
cache_key = self._current_command_cache_entry.cache_key
15341534
if response is None:
15351535
self._cache.delete_by_cache_keys([cache_key])
1536+
self._current_command_cache_entry = None
15361537
return response
15371538

1538-
cache_entry = self._cache.get(cache_key)
1539-
1540-
# Cache only responses that still valid
1541-
# and wasn't invalidated by another connection in meantime.
1542-
if cache_entry is not None:
1543-
cache_entry.status = CacheEntryStatus.VALID
1544-
cache_entry.cache_value = response
1545-
self._cache.set(cache_entry)
1546-
1539+
# No bother entry exists in cache or not, this ensures we don't overwrite another entry.
1540+
self._current_command_cache_entry.status = CacheEntryStatus.VALID
1541+
self._current_command_cache_entry.cache_value = response
15471542
self._current_command_cache_entry = None
15481543

15491544
return response

tests/test_connection.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,6 @@ def test_read_response_returns_cached_reply(self, mock_cache, mock_connection):
483483
mock_cache.is_cachable.return_value = True
484484
mock_cache.get.side_effect = [
485485
None,
486-
CacheEntry(
487-
cache_key=CacheKey(
488-
command="GET", redis_keys=("foo",), redis_args=("GET", "foo")
489-
),
490-
cache_value=CacheProxyConnection.DUMMY_CACHE_VALUE,
491-
status=CacheEntryStatus.IN_PROGRESS,
492-
connection_ref=mock_connection,
493-
),
494486
CacheEntry(
495487
cache_key=CacheKey(
496488
command="GET", redis_keys=("foo",), redis_args=("GET", "foo")
@@ -526,22 +518,14 @@ def test_read_response_returns_cached_reply(self, mock_cache, mock_connection):
526518
proxy_connection.send_command(*["GET", "foo"], **{"keys": ["foo"]})
527519
assert proxy_connection.read_response() == b"bar"
528520
assert proxy_connection._current_command_cache_entry is None
521+
522+
# cached reply
523+
proxy_connection.send_command(*["GET", "foo"], **{"keys": ["foo"]})
529524
assert proxy_connection.read_response() == b"bar"
525+
assert proxy_connection._current_command_cache_entry is None
530526

531527
mock_cache.set.assert_has_calls(
532528
[
533-
call(
534-
CacheEntry(
535-
cache_key=CacheKey(
536-
command="GET",
537-
redis_keys=("foo",),
538-
redis_args=("GET", "foo"),
539-
),
540-
cache_value=CacheProxyConnection.DUMMY_CACHE_VALUE,
541-
status=CacheEntryStatus.IN_PROGRESS,
542-
connection_ref=mock_connection,
543-
)
544-
),
545529
call(
546530
CacheEntry(
547531
cache_key=CacheKey(

0 commit comments

Comments
 (0)