-
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
+492
−82
Merged
Changes from 2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import random | ||
import time | ||
from collections import OrderedDict, defaultdict | ||
|
||
|
||
class _Cache: | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def __init__(self, max_size: int, ttl: int, eviction_policy: str, **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, response, keys_in_command): | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(self.cache) >= self.max_size: | ||
self._evict() | ||
self.cache[command] = { | ||
"response": response, | ||
"keys": keys_in_command, | ||
"created_time": time.monotonic(), | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if self.eviction_policy == "lfu": | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.cache[command]["access_count"] = 0 | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._update_key_commands_map(keys_in_command, command) | ||
self.commands_ttl_list.append(command) | ||
|
||
def get(self, command): | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if command in self.cache: | ||
if self._is_expired(command): | ||
del self.cache[command] | ||
keys_in_command = self.cache[command]["keys"] | ||
self._del_key_commands_map(keys_in_command, command) | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return None | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._update_access(command) | ||
return self.cache[command]["response"] | ||
return None | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def delete(self, command): | ||
if command in self.cache: | ||
keys_in_command = self.cache[command]["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): | ||
self.cache.clear() | ||
self.key_commands_map.clear() | ||
self.commands_ttl_list = [] | ||
|
||
def _is_expired(self, command): | ||
if self.ttl == 0: | ||
return False | ||
return time.monotonic() - self.cache[command]["created_time"] > self.ttl | ||
|
||
def _update_access(self, command): | ||
if self.eviction_policy == "lru": | ||
self.cache.move_to_end(command) | ||
elif self.eviction_policy == "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 == "random": | ||
pass # Random eviction doesn't require updates | ||
|
||
def _evict(self): | ||
if self._is_expired(self.commands_ttl_list[0]): | ||
self.delete(self.commands_ttl_list[0]) | ||
elif self.eviction_policy == "lru": | ||
self.cache.popitem(last=False) | ||
elif self.eviction_policy == "lfu": | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 == "random": | ||
random_command = random.choice(list(self.cache.keys())) | ||
self.cache.pop(random_command) | ||
|
||
def _update_key_commands_map(self, keys, 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, command): | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for key in keys: | ||
self.key_commands_map[key].remove(command) | ||
|
||
def invalidate(self, key): | ||
if key in self.key_commands_map: | ||
for command in self.key_commands_map[key]: | ||
dvora-h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.delete(command) |
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
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.
To keep in theme we discussed, maybe this should be redis.cache.LocalCache?