Skip to content

Commit 48607e9

Browse files
committed
Temporary refactor
1 parent 629d3db commit 48607e9

File tree

3 files changed

+433
-89
lines changed

3 files changed

+433
-89
lines changed

redis/cache.py

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Callable, TypeVar, Any, NoReturn, List, Union
22
from typing import Optional
3+
from enum import Enum
34

45
from cachetools import TTLCache, Cache, LRUCache
56
from cachetools.keys import hashkey
@@ -9,6 +10,110 @@
910
T = TypeVar('T')
1011

1112

13+
class EvictionPolicy(Enum):
14+
LRU = "lru"
15+
LFU = "lfu"
16+
RANDOM = "random"
17+
18+
19+
class CacheConfiguration:
20+
DEFAULT_EVICTION_POLICY = EvictionPolicy.LRU
21+
22+
DEFAULT_ALLOW_LIST = [
23+
"BITCOUNT",
24+
"BITFIELD_RO",
25+
"BITPOS",
26+
"EXISTS",
27+
"GEODIST",
28+
"GEOHASH",
29+
"GEOPOS",
30+
"GEORADIUSBYMEMBER_RO",
31+
"GEORADIUS_RO",
32+
"GEOSEARCH",
33+
"GET",
34+
"GETBIT",
35+
"GETRANGE",
36+
"HEXISTS",
37+
"HGET",
38+
"HGETALL",
39+
"HKEYS",
40+
"HLEN",
41+
"HMGET",
42+
"HSTRLEN",
43+
"HVALS",
44+
"JSON.ARRINDEX",
45+
"JSON.ARRLEN",
46+
"JSON.GET",
47+
"JSON.MGET",
48+
"JSON.OBJKEYS",
49+
"JSON.OBJLEN",
50+
"JSON.RESP",
51+
"JSON.STRLEN",
52+
"JSON.TYPE",
53+
"LCS",
54+
"LINDEX",
55+
"LLEN",
56+
"LPOS",
57+
"LRANGE",
58+
"MGET",
59+
"SCARD",
60+
"SDIFF",
61+
"SINTER",
62+
"SINTERCARD",
63+
"SISMEMBER",
64+
"SMEMBERS",
65+
"SMISMEMBER",
66+
"SORT_RO",
67+
"STRLEN",
68+
"SUBSTR",
69+
"SUNION",
70+
"TS.GET",
71+
"TS.INFO",
72+
"TS.RANGE",
73+
"TS.REVRANGE",
74+
"TYPE",
75+
"XLEN",
76+
"XPENDING",
77+
"XRANGE",
78+
"XREAD",
79+
"XREVRANGE",
80+
"ZCARD",
81+
"ZCOUNT",
82+
"ZDIFF",
83+
"ZINTER",
84+
"ZINTERCARD",
85+
"ZLEXCOUNT",
86+
"ZMSCORE",
87+
"ZRANGE",
88+
"ZRANGEBYLEX",
89+
"ZRANGEBYSCORE",
90+
"ZRANK",
91+
"ZREVRANGE",
92+
"ZREVRANGEBYLEX",
93+
"ZREVRANGEBYSCORE",
94+
"ZREVRANK",
95+
"ZSCORE",
96+
"ZUNION",
97+
]
98+
99+
def __init__(self, **kwargs):
100+
self._max_size = kwargs.get("cache_size", 10000)
101+
self._ttl = kwargs.get("cache_ttl", 0)
102+
self._eviction_policy = kwargs.get("eviction_policy", self.DEFAULT_EVICTION_POLICY)
103+
104+
def get_ttl(self) -> int:
105+
return self._ttl
106+
107+
def get_eviction_policy(self) -> EvictionPolicy:
108+
return self._eviction_policy
109+
110+
def is_exceeds_max_size(self, count: int) -> bool:
111+
return count > self._max_size
112+
113+
def is_allowed_to_cache(self, command: str) -> bool:
114+
return command in self.DEFAULT_ALLOW_LIST
115+
116+
12117
def ensure_string(key):
13118
if isinstance(key, bytes):
14119
return key.decode('utf-8')
@@ -118,7 +223,7 @@ def _on_connect(self, conn):
118223
conn._parser.set_invalidation_push_handler(self._cache_invalidation_process)
119224

120225
def _cache_invalidation_process(
121-
self, data: List[Union[str, Optional[List[str]]]]
226+
self, data: List[Union[str, Optional[List[str]]]]
122227
) -> None:
123228
"""
124229
Invalidate (delete) all redis commands associated with a specific key.

redis/client.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ def __init__(
311311
"ssl_ciphers": ssl_ciphers,
312312
}
313313
)
314+
if use_cache and protocol in [3, "3"]:
315+
kwargs.update(
316+
{
317+
"use_cache": use_cache,
318+
"cache": cache,
319+
"cache_size": cache_size,
320+
"cache_ttl": cache_ttl,
321+
}
322+
)
314323
connection_pool = ConnectionPool(**kwargs)
315324
self.auto_close_connection_pool = True
316325
else:
@@ -320,7 +329,6 @@ def __init__(
320329

321330
if use_cache and self.connection_pool.get_protocol() not in [3, "3"]:
322331
raise RedisError("Client caching is only supported with RESP version 3")
323-
CacheMixin.__init__(self, use_cache, self.connection_pool, cache, cache_size, cache_ttl)
324332

325333
self.connection = None
326334
if single_connection_client:
@@ -535,7 +543,7 @@ def _send_command_parse_response(self, conn, command_name, *args, **options):
535543
"""
536544
Send a command and parse the response
537545
"""
538-
conn.send_command(*args)
546+
conn.send_command(*args, **options)
539547
return self.parse_response(conn, command_name, **options)
540548

541549
def _disconnect_raise(self, conn, error):
@@ -553,8 +561,6 @@ def _disconnect_raise(self, conn, error):
553561

554562
# COMMAND EXECUTION AND PROTOCOL PARSING
555563
def execute_command(self, *args, **options):
556-
if self.use_cache:
557-
return self.cached_call(self._execute_command, *args, **options)
558564
return self._execute_command(*args, **options)
559565

560566
def _execute_command(self, *args, **options):

0 commit comments

Comments
 (0)