-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Client Side Caching: Alpha support #3038
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd27dc3
CSC
dvora-h 6c69ac4
get keys from command
dvora-h 2973a53
fix review comments
dvora-h ec73468
return respone in execute_command
dvora-h a8780fb
fix tests
dvora-h ea5ecfc
Merge branch 'master' into csc
chayim e06d140
fix comments
dvora-h 63bb591
linters
dvora-h File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,326 @@ | ||
import random | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To keep in theme we discussed, maybe this should be redis.cache.LocalCache? |
||
import time | ||
from collections import OrderedDict, defaultdict | ||
from enum import Enum | ||
from typing import List | ||
|
||
from redis.typing import KeyT, ResponseT | ||
|
||
DEFAULT_EVICTION_POLICY = "lru" | ||
|
||
|
||
DEFAULT_BLACKLIST = [ | ||
"BF.CARD", | ||
"BF.DEBUG", | ||
"BF.EXISTS", | ||
"BF.INFO", | ||
"BF.MEXISTS", | ||
"BF.SCANDUMP", | ||
"CF.COMPACT", | ||
"CF.COUNT", | ||
"CF.DEBUG", | ||
"CF.EXISTS", | ||
"CF.INFO", | ||
"CF.MEXISTS", | ||
"CF.SCANDUMP", | ||
"CMS.INFO", | ||
"CMS.QUERY", | ||
"DUMP", | ||
"EXPIRETIME", | ||
"FT.AGGREGATE", | ||
"FT.ALIASADD", | ||
"FT.ALIASDEL", | ||
"FT.ALIASUPDATE", | ||
"FT.CURSOR", | ||
"FT.EXPLAIN", | ||
"FT.EXPLAINCLI", | ||
"FT.GET", | ||
"FT.INFO", | ||
"FT.MGET", | ||
"FT.PROFILE", | ||
"FT.SEARCH", | ||
"FT.SPELLCHECK", | ||
"FT.SUGGET", | ||
"FT.SUGLEN", | ||
"FT.SYNDUMP", | ||
"FT.TAGVALS", | ||
"FT._ALIASADDIFNX", | ||
"FT._ALIASDELIFX", | ||
"HRANDFIELD", | ||
"JSON.DEBUG", | ||
"PEXPIRETIME", | ||
"PFCOUNT", | ||
"PTTL", | ||
"SRANDMEMBER", | ||
"TDIGEST.BYRANK", | ||
"TDIGEST.BYREVRANK", | ||
"TDIGEST.CDF", | ||
"TDIGEST.INFO", | ||
"TDIGEST.MAX", | ||
"TDIGEST.MIN", | ||
"TDIGEST.QUANTILE", | ||
"TDIGEST.RANK", | ||
"TDIGEST.REVRANK", | ||
"TDIGEST.TRIMMED_MEAN", | ||
"TOPK.INFO", | ||
"TOPK.LIST", | ||
"TOPK.QUERY", | ||
"TOUCH", | ||
"TTL", | ||
] | ||
|
||
|
||
DEFAULT_WHITELIST = [ | ||
"BITCOUNT", | ||
"BITFIELD_RO", | ||
"BITPOS", | ||
"EXISTS", | ||
"GEODIST", | ||
"GEOHASH", | ||
"GEOPOS", | ||
"GEORADIUSBYMEMBER_RO", | ||
"GEORADIUS_RO", | ||
"GEOSEARCH", | ||
"GET", | ||
"GETBIT", | ||
"GETRANGE", | ||
"HEXISTS", | ||
"HGET", | ||
"HGETALL", | ||
"HKEYS", | ||
"HLEN", | ||
"HMGET", | ||
"HSTRLEN", | ||
"HVALS", | ||
"JSON.ARRINDEX", | ||
"JSON.ARRLEN", | ||
"JSON.GET", | ||
"JSON.MGET", | ||
"JSON.OBJKEYS", | ||
"JSON.OBJLEN", | ||
"JSON.RESP", | ||
"JSON.STRLEN", | ||
"JSON.TYPE", | ||
"LCS", | ||
"LINDEX", | ||
"LLEN", | ||
"LPOS", | ||
"LRANGE", | ||
"MGET", | ||
"SCARD", | ||
"SDIFF", | ||
"SINTER", | ||
"SINTERCARD", | ||
"SISMEMBER", | ||
"SMEMBERS", | ||
"SMISMEMBER", | ||
"SORT_RO", | ||
"STRLEN", | ||
"SUBSTR", | ||
"SUNION", | ||
"TS.GET", | ||
"TS.INFO", | ||
"TS.RANGE", | ||
"TS.REVRANGE", | ||
"TYPE", | ||
"XLEN", | ||
"XPENDING", | ||
"XRANGE", | ||
"XREAD", | ||
"XREVRANGE", | ||
"ZCARD", | ||
"ZCOUNT", | ||
"ZDIFF", | ||
"ZINTER", | ||
"ZINTERCARD", | ||
"ZLEXCOUNT", | ||
"ZMSCORE", | ||
"ZRANGE", | ||
"ZRANGEBYLEX", | ||
"ZRANGEBYSCORE", | ||
"ZRANK", | ||
"ZREVRANGE", | ||
"ZREVRANGEBYLEX", | ||
"ZREVRANGEBYSCORE", | ||
"ZREVRANK", | ||
"ZSCORE", | ||
"ZUNION", | ||
] | ||
|
||
_RESPONSE = "response" | ||
_KEYS = "keys" | ||
_CTIME = "ctime" | ||
_ACCESS_COUNT = "access_count" | ||
|
||
|
||
class EvictionPolicy(Enum): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. awesome |
||
LRU = "lru" | ||
LFU = "lfu" | ||
RANDOM = "random" | ||
|
||
|
||
class _Cache: | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
A caching mechanism for storing redis commands and their responses. | ||
|
||
Args: | ||
max_size (int): The maximum number of commands to be stored in the cache. | ||
ttl (int): The time-to-live for each command in seconds. | ||
eviction_policy (EvictionPolicy): The eviction policy to use for removing commands when the cache is full. | ||
|
||
Attributes: | ||
max_size (int): The maximum number of commands to be stored in the cache. | ||
ttl (int): The time-to-live for each command in seconds. | ||
eviction_policy (EvictionPolicy): The eviction policy used for cache management. | ||
cache (OrderedDict): The ordered dictionary to store commands and their metadata. | ||
key_commands_map (defaultdict): A mapping of keys to the set of commands that use each key. | ||
commands_ttl_list (list): A list to keep track of the commands in the order they were added. # noqa | ||
""" | ||
|
||
def __init__( | ||
self, max_size: int, ttl: int, eviction_policy: EvictionPolicy, **kwargs | ||
): | ||
self.max_size = max_size | ||
self.ttl = ttl | ||
self.eviction_policy = eviction_policy | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.cache = OrderedDict() | ||
self.key_commands_map = defaultdict(set) | ||
self.commands_ttl_list = [] | ||
|
||
def set(self, command: str, response: ResponseT, keys_in_command: List[KeyT]): | ||
""" | ||
Set a redis command and its response in the cache. | ||
|
||
Args: | ||
command (str): The redis command. | ||
response (ResponseT): The response associated with the command. | ||
keys_in_command (List[KeyT]): The list of keys used in the command. | ||
""" | ||
if len(self.cache) >= self.max_size: | ||
self._evict() | ||
self.cache[command] = { | ||
_RESPONSE: response, | ||
_KEYS: keys_in_command, | ||
_CTIME: time.monotonic(), | ||
_ACCESS_COUNT: 0, # Used only for LFU | ||
} | ||
self._update_key_commands_map(keys_in_command, command) | ||
self.commands_ttl_list.append(command) | ||
|
||
def get(self, command: str) -> ResponseT: | ||
""" | ||
Get the response for a redis command from the cache. | ||
|
||
Args: | ||
command (str): The redis command. | ||
|
||
Returns: | ||
ResponseT: The response associated with the command, or None if the command is not in the cache. # noqa | ||
""" | ||
if command in self.cache: | ||
if self._is_expired(command): | ||
self.delete(command) | ||
self._update_access(command) | ||
return self.cache[command]["response"] | ||
|
||
def delete(self, command: str): | ||
""" | ||
Delete a redis command and its metadata from the cache. | ||
|
||
Args: | ||
command (str): The redis command to be deleted. | ||
""" | ||
if command in self.cache: | ||
keys_in_command = self.cache[command].get("keys") | ||
self._del_key_commands_map(keys_in_command, command) | ||
self.commands_ttl_list.remove(command) | ||
del self.cache[command] | ||
|
||
def delete_many(self, commands): | ||
pass | ||
|
||
def flush(self): | ||
"""Clear the entire cache, removing all redis commands and metadata.""" | ||
self.cache.clear() | ||
self.key_commands_map.clear() | ||
self.commands_ttl_list = [] | ||
|
||
def _is_expired(self, command: str) -> bool: | ||
""" | ||
Check if a redis command has expired based on its time-to-live. | ||
|
||
Args: | ||
command (str): The redis command. | ||
|
||
Returns: | ||
bool: True if the command has expired, False otherwise. | ||
""" | ||
if self.ttl == 0: | ||
return False | ||
return time.monotonic() - self.cache[command]["ctime"] > self.ttl | ||
|
||
def _update_access(self, command: str): | ||
""" | ||
Update the access information for a redis command based on the eviction policy. | ||
|
||
Args: | ||
command (str): The redis command. | ||
""" | ||
if self.eviction_policy == EvictionPolicy.LRU: | ||
self.cache.move_to_end(command) | ||
elif self.eviction_policy == EvictionPolicy.LFU: | ||
self.cache[command]["access_count"] = ( | ||
self.cache.get(command, {}).get("access_count", 0) + 1 | ||
) | ||
self.cache.move_to_end(command) | ||
elif self.eviction_policy == EvictionPolicy.RANDOM: | ||
pass # Random eviction doesn't require updates | ||
|
||
def _evict(self): | ||
"""Evict a redis command from the cache based on the eviction policy.""" | ||
if self._is_expired(self.commands_ttl_list[0]): | ||
self.delete(self.commands_ttl_list[0]) | ||
elif self.eviction_policy == EvictionPolicy.LRU: | ||
self.cache.popitem(last=False) | ||
elif self.eviction_policy == EvictionPolicy.LFU: | ||
min_access_command = min( | ||
self.cache, key=lambda k: self.cache[k].get("access_count", 0) | ||
) | ||
self.cache.pop(min_access_command) | ||
elif self.eviction_policy == EvictionPolicy.RANDOM: | ||
random_command = random.choice(list(self.cache.keys())) | ||
self.cache.pop(random_command) | ||
|
||
def _update_key_commands_map(self, keys: List[KeyT], command: str): | ||
""" | ||
Update the key_commands_map with command that uses the keys. | ||
|
||
Args: | ||
keys (List[KeyT]): The list of keys used in the command. | ||
command (str): The redis command. | ||
""" | ||
for key in keys: | ||
self.key_commands_map[key].add(command) | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def _del_key_commands_map(self, keys: List[KeyT], command: str): | ||
""" | ||
Remove a redis command from the key_commands_map. | ||
|
||
Args: | ||
keys (List[KeyT]): The list of keys used in the redis command. | ||
command (str): The redis command. | ||
""" | ||
for key in keys: | ||
self.key_commands_map[key].remove(command) | ||
|
||
def invalidate(self, key: KeyT): | ||
""" | ||
Invalidate (delete) all redis commands associated with a specific key. | ||
|
||
Args: | ||
key (KeyT): The key to be invalidated. | ||
""" | ||
if key not in self.key_commands_map: | ||
return | ||
for command in self.key_commands_map[key]: | ||
self.delete(command) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"the keys are used only for client side caching (thoughout this PR)