23
23
cast ,
24
24
)
25
25
26
+ from redis ._cache import (
27
+ DEFAULT_BLACKLIST ,
28
+ DEFAULT_EVICTION_POLICY ,
29
+ DEFAULT_WHITELIST ,
30
+ _LocalCache ,
31
+ )
26
32
from redis ._parsers .helpers import (
27
33
_RedisCallbacks ,
28
34
_RedisCallbacksRESP2 ,
37
43
)
38
44
from redis .asyncio .lock import Lock
39
45
from redis .asyncio .retry import Retry
40
- from redis .cache import (
41
- DEFAULT_BLACKLIST ,
42
- DEFAULT_EVICTION_POLICY ,
43
- DEFAULT_WHITELIST ,
44
- _LocalCache ,
45
- )
46
46
from redis .client import (
47
47
EMPTY_RESPONSE ,
48
48
NEVER_DECODE ,
66
66
TimeoutError ,
67
67
WatchError ,
68
68
)
69
- from redis .typing import ChannelT , EncodableT , KeysT , KeyT , ResponseT
69
+ from redis .typing import ChannelT , EncodableT , KeyT
70
70
from redis .utils import (
71
71
HIREDIS_AVAILABLE ,
72
72
_set_info_logger ,
@@ -293,6 +293,13 @@ def __init__(
293
293
"lib_version" : lib_version ,
294
294
"redis_connect_func" : redis_connect_func ,
295
295
"protocol" : protocol ,
296
+ "cache_enable" : cache_enable ,
297
+ "client_cache" : client_cache ,
298
+ "cache_max_size" : cache_max_size ,
299
+ "cache_ttl" : cache_ttl ,
300
+ "cache_eviction_policy" : cache_eviction_policy ,
301
+ "cache_blacklist" : cache_blacklist ,
302
+ "cache_whitelist" : cache_whitelist ,
296
303
}
297
304
# based on input, setup appropriate connection args
298
305
if unix_socket_path is not None :
@@ -349,16 +356,6 @@ def __init__(
349
356
# on a set of redis commands
350
357
self ._single_conn_lock = asyncio .Lock ()
351
358
352
- self .client_cache = client_cache
353
- if cache_enable :
354
- self .client_cache = _LocalCache (
355
- cache_max_size , cache_ttl , cache_eviction_policy
356
- )
357
- if self .client_cache is not None :
358
- self .cache_blacklist = cache_blacklist
359
- self .cache_whitelist = cache_whitelist
360
- self .client_cache_initialized = False
361
-
362
359
def __repr__ (self ):
363
360
return f"{ self .__class__ .__name__ } <{ self .connection_pool !r} >"
364
361
@@ -370,10 +367,6 @@ async def initialize(self: _RedisT) -> _RedisT:
370
367
async with self ._single_conn_lock :
371
368
if self .connection is None :
372
369
self .connection = await self .connection_pool .get_connection ("_" )
373
- if self .client_cache is not None :
374
- self .connection ._parser .set_invalidation_push_handler (
375
- self ._cache_invalidation_process
376
- )
377
370
return self
378
371
379
372
def set_response_callback (self , command : str , callback : ResponseCallbackT ):
@@ -592,8 +585,6 @@ async def aclose(self, close_connection_pool: Optional[bool] = None) -> None:
592
585
close_connection_pool is None and self .auto_close_connection_pool
593
586
):
594
587
await self .connection_pool .disconnect ()
595
- if self .client_cache :
596
- self .client_cache .flush ()
597
588
598
589
@deprecated_function (version = "5.0.1" , reason = "Use aclose() instead" , name = "close" )
599
590
async def close (self , close_connection_pool : Optional [bool ] = None ) -> None :
@@ -622,89 +613,28 @@ async def _disconnect_raise(self, conn: Connection, error: Exception):
622
613
):
623
614
raise error
624
615
625
- def _cache_invalidation_process (
626
- self , data : List [Union [str , Optional [List [str ]]]]
627
- ) -> None :
628
- """
629
- Invalidate (delete) all redis commands associated with a specific key.
630
- `data` is a list of strings, where the first string is the invalidation message
631
- and the second string is the list of keys to invalidate.
632
- (if the list of keys is None, then all keys are invalidated)
633
- """
634
- if data [1 ] is not None :
635
- for key in data [1 ]:
636
- self .client_cache .invalidate (str_if_bytes (key ))
637
- else :
638
- self .client_cache .flush ()
639
-
640
- async def _get_from_local_cache (self , command : str ):
641
- """
642
- If the command is in the local cache, return the response
643
- """
644
- if (
645
- self .client_cache is None
646
- or command [0 ] in self .cache_blacklist
647
- or command [0 ] not in self .cache_whitelist
648
- ):
649
- return None
650
- while not self .connection ._is_socket_empty ():
651
- await self .connection .read_response (push_request = True )
652
- return self .client_cache .get (command )
653
-
654
- def _add_to_local_cache (
655
- self , command : Tuple [str ], response : ResponseT , keys : List [KeysT ]
656
- ):
657
- """
658
- Add the command and response to the local cache if the command
659
- is allowed to be cached
660
- """
661
- if (
662
- self .client_cache is not None
663
- and (self .cache_blacklist == [] or command [0 ] not in self .cache_blacklist )
664
- and (self .cache_whitelist == [] or command [0 ] in self .cache_whitelist )
665
- ):
666
- self .client_cache .set (command , response , keys )
667
-
668
- def delete_from_local_cache (self , command : str ):
669
- """
670
- Delete the command from the local cache
671
- """
672
- try :
673
- self .client_cache .delete (command )
674
- except AttributeError :
675
- pass
676
-
677
616
# COMMAND EXECUTION AND PROTOCOL PARSING
678
617
async def execute_command (self , * args , ** options ):
679
618
"""Execute a command and return a parsed response"""
680
619
await self .initialize ()
681
620
command_name = args [0 ]
682
621
keys = options .pop ("keys" , None ) # keys are used only for client side caching
683
- response_from_cache = await self ._get_from_local_cache (args )
622
+ pool = self .connection_pool
623
+ conn = self .connection or await pool .get_connection (command_name , ** options )
624
+ response_from_cache = await conn ._get_from_local_cache (args )
684
625
if response_from_cache is not None :
685
626
return response_from_cache
686
627
else :
687
- pool = self .connection_pool
688
- conn = self .connection or await pool .get_connection (command_name , ** options )
689
-
690
628
if self .single_connection_client :
691
629
await self ._single_conn_lock .acquire ()
692
630
try :
693
- if self .client_cache is not None and not self .client_cache_initialized :
694
- await conn .retry .call_with_retry (
695
- lambda : self ._send_command_parse_response (
696
- conn , "CLIENT" , * ("CLIENT" , "TRACKING" , "ON" )
697
- ),
698
- lambda error : self ._disconnect_raise (conn , error ),
699
- )
700
- self .client_cache_initialized = True
701
631
response = await conn .retry .call_with_retry (
702
632
lambda : self ._send_command_parse_response (
703
633
conn , command_name , * args , ** options
704
634
),
705
635
lambda error : self ._disconnect_raise (conn , error ),
706
636
)
707
- self ._add_to_local_cache (args , response , keys )
637
+ conn ._add_to_local_cache (args , response , keys )
708
638
return response
709
639
finally :
710
640
if self .single_connection_client :
0 commit comments