Skip to content
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 8 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions redis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from redis import asyncio # noqa
from redis.backoff import default_backoff
from redis.cache import _Cache
from redis.client import Redis, StrictRedis
from redis.cluster import RedisCluster
from redis.connection import (
Expand Down Expand Up @@ -61,6 +62,7 @@ def int_or_str(value):
VERSION = tuple([99, 99, 99])

__all__ = [
"_Cache",
"AuthenticationError",
"AuthenticationWrongNumberOfArgsError",
"BlockingConnectionPool",
Expand Down
95 changes: 95 additions & 0 deletions redis/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import random
Copy link
Contributor

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?

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)
67 changes: 55 additions & 12 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
_RedisCallbacksRESP3,
bool_ok,
)
from redis.cache import _Cache
from redis.commands import (
CoreCommands,
RedisModuleCommands,
Expand All @@ -33,6 +34,8 @@
from redis.lock import Lock
from redis.retry import Retry
from redis.utils import (
DEFAULT_BLACKLIST,
DEFAULT_WHITELIST,
HIREDIS_AVAILABLE,
_set_info_logger,
get_lib_version,
Expand Down Expand Up @@ -203,6 +206,13 @@ def __init__(
redis_connect_func=None,
credential_provider: Optional[CredentialProvider] = None,
protocol: Optional[int] = 2,
client_caching: bool = False,
client_cache: Optional[_Cache] = None,
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
cache_max_size: int = 100,
cache_ttl: int = 0,
cache_eviction_policy: str = "lru",
cache_blacklist: List[str] = DEFAULT_BLACKLIST,
cache_whitelist: List[str] = DEFAULT_WHITELIST,
) -> None:
"""
Initialize a new Redis client.
Expand Down Expand Up @@ -310,6 +320,12 @@ def __init__(
else:
self.response_callbacks.update(_RedisCallbacksRESP2)

self.client_cache = client_cache
self.cache_blacklist = cache_blacklist
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
self.cache_whitelist = cache_whitelist
if client_caching:
self.client_cache = _Cache(cache_max_size, cache_ttl, cache_eviction_policy)

def __repr__(self) -> str:
return f"{type(self).__name__}<{repr(self.connection_pool)}>"

Expand Down Expand Up @@ -525,23 +541,50 @@ def _disconnect_raise(self, conn, error):
):
raise error

def get_from_local_cache(self, command):
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
if (
self.client_cache is None
or command[0] in self.cache_blacklist
or command[0] not in self.cache_whitelist
):
return None
return self.client_cache.get(command)

def add_to_local_cache(self, command, response, keys):
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
if (
self.client_cache is not None
and (self.cache_blacklist == [] or command[0] not in self.cache_blacklist)
and (self.cache_whitelist == [] or command[0] in self.cache_whitelist)
):
self.client_cache.set(command, response, keys)

def delete_from_local_cache(self, command):
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
if self.client_cache is not None:
self.client_cache.delete(command)
dvora-h marked this conversation as resolved.
Show resolved Hide resolved

# COMMAND EXECUTION AND PROTOCOL PARSING
def execute_command(self, *args, **options):
"""Execute a command and return a parsed response"""
pool = self.connection_pool
command_name = args[0]
conn = self.connection or pool.get_connection(command_name, **options)
keys = options.pop("keys", None)
response_from_cache = self.get_from_local_cache(args)
if response_from_cache is not None:
return response_from_cache
else:
pool = self.connection_pool
conn = self.connection or pool.get_connection(command_name, **options)

try:
return conn.retry.call_with_retry(
lambda: self._send_command_parse_response(
conn, command_name, *args, **options
),
lambda error: self._disconnect_raise(conn, error),
)
finally:
if not self.connection:
pool.release(conn)
try:
response = conn.retry.call_with_retry(
lambda: self._send_command_parse_response(
conn, command_name, *args, **options
),
lambda error: self._disconnect_raise(conn, error),
)
self.add_to_local_cache(args, response, keys)
finally:
if not self.connection:
pool.release(conn)

def parse_response(self, connection, command_name, **options):
"""Parses a response from the Redis server"""
Expand Down
Loading
Loading