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