|
54 | 54 | DEFAULT_BLACKLIST,
|
55 | 55 | DEFAULT_EVICTION_POLICY,
|
56 | 56 | DEFAULT_WHITELIST,
|
| 57 | + AbstractCache, |
57 | 58 | _LocalCache,
|
58 | 59 | )
|
59 | 60 | from .._parsers import (
|
@@ -157,11 +158,11 @@ def __init__(
|
157 | 158 | encoder_class: Type[Encoder] = Encoder,
|
158 | 159 | credential_provider: Optional[CredentialProvider] = None,
|
159 | 160 | protocol: Optional[int] = 2,
|
160 |
| - cache_enable: bool = False, |
161 |
| - client_cache: Optional[_LocalCache] = None, |
162 |
| - cache_max_size: int = 100, |
| 161 | + cache_enabled: bool = False, |
| 162 | + client_cache: Optional[AbstractCache] = None, |
| 163 | + cache_max_size: int = 10000, |
163 | 164 | cache_ttl: int = 0,
|
164 |
| - cache_eviction_policy: str = DEFAULT_EVICTION_POLICY, |
| 165 | + cache_policy: str = DEFAULT_EVICTION_POLICY, |
165 | 166 | cache_blacklist: List[str] = DEFAULT_BLACKLIST,
|
166 | 167 | cache_whitelist: List[str] = DEFAULT_WHITELIST,
|
167 | 168 | ):
|
@@ -221,8 +222,8 @@ def __init__(
|
221 | 222 | if p < 2 or p > 3:
|
222 | 223 | raise ConnectionError("protocol must be either 2 or 3")
|
223 | 224 | self.protocol = protocol
|
224 |
| - if cache_enable: |
225 |
| - _cache = _LocalCache(cache_max_size, cache_ttl, cache_eviction_policy) |
| 225 | + if cache_enabled: |
| 226 | + _cache = _LocalCache(cache_max_size, cache_ttl, cache_policy) |
226 | 227 | else:
|
227 | 228 | _cache = None
|
228 | 229 | self.client_cache = client_cache if client_cache is not None else _cache
|
@@ -699,7 +700,7 @@ def _cache_invalidation_process(
|
699 | 700 | self.client_cache.flush()
|
700 | 701 | else:
|
701 | 702 | for key in data[1]:
|
702 |
| - self.client_cache.invalidate(str_if_bytes(key)) |
| 703 | + self.client_cache.invalidate_key(str_if_bytes(key)) |
703 | 704 |
|
704 | 705 | async def _get_from_local_cache(self, command: str):
|
705 | 706 | """
|
@@ -729,15 +730,6 @@ def _add_to_local_cache(
|
729 | 730 | ):
|
730 | 731 | self.client_cache.set(command, response, keys)
|
731 | 732 |
|
732 |
| - def delete_from_local_cache(self, command: str): |
733 |
| - """ |
734 |
| - Delete the command from the local cache |
735 |
| - """ |
736 |
| - try: |
737 |
| - self.client_cache.delete(command) |
738 |
| - except AttributeError: |
739 |
| - pass |
740 |
| - |
741 | 733 |
|
742 | 734 | class Connection(AbstractConnection):
|
743 | 735 | "Manages TCP communication to and from a Redis server"
|
@@ -1241,6 +1233,36 @@ def set_retry(self, retry: "Retry") -> None:
|
1241 | 1233 | for conn in self._in_use_connections:
|
1242 | 1234 | conn.retry = retry
|
1243 | 1235 |
|
| 1236 | + def flush_cache(self): |
| 1237 | + connections = chain(self._available_connections, self._in_use_connections) |
| 1238 | + |
| 1239 | + for connection in connections: |
| 1240 | + try: |
| 1241 | + connection.client_cache.flush() |
| 1242 | + except AttributeError: |
| 1243 | + # cache is not enabled |
| 1244 | + pass |
| 1245 | + |
| 1246 | + def delete_command_from_cache(self, command: str): |
| 1247 | + connections = chain(self._available_connections, self._in_use_connections) |
| 1248 | + |
| 1249 | + for connection in connections: |
| 1250 | + try: |
| 1251 | + connection.client_cache.delete_command(command) |
| 1252 | + except AttributeError: |
| 1253 | + # cache is not enabled |
| 1254 | + pass |
| 1255 | + |
| 1256 | + def invalidate_key_from_cache(self, key: str): |
| 1257 | + connections = chain(self._available_connections, self._in_use_connections) |
| 1258 | + |
| 1259 | + for connection in connections: |
| 1260 | + try: |
| 1261 | + connection.client_cache.invalidate_key(key) |
| 1262 | + except AttributeError: |
| 1263 | + # cache is not enabled |
| 1264 | + pass |
| 1265 | + |
1244 | 1266 |
|
1245 | 1267 | class BlockingConnectionPool(ConnectionPool):
|
1246 | 1268 | """
|
|
0 commit comments