Caching¶
coredis.patterns.cache
Built in caches¶
- class LRUCache(max_keys: int = 2**12, confidence: float = 100, dynamic_confidence: bool = False)[source]¶
Implementation of an LRU cache that can be used with
Redisorcoredis.RedisCluster- Parameters:
max_keys¶ – maximum keys to cache. A negative value represents and unbounded cache.
confidence¶ – 0 - 100. Lower values will result in the client discarding and / or validating the cached responses
dynamic_confidence¶ – Whether to adjust the confidence based on sampled validations. Tainted values drop the confidence by 0.1% and confirmations of correct cached values will increase the confidence by 0.01% upto 100.
- put(command: bytes, key: RedisValueT, *args: RedisValueT, value: ResponseType) None[source]¶
Cache the response for command/key/args combination
- get(command: bytes, key: RedisValueT, *args: RedisValueT) ResponseType[source]¶
Fetch the cached response for command/key/args combination
- invalidate(*keys: RedisValueT) None[source]¶
Invalidate any cached entries for the provided keys
- property stats: CacheStats¶
Returns the current stats for the cache
- property confidence: float¶
Confidence in cached values between 0 - 100. Lower values will result in the client discarding and / or validating the cached responses
- feedback(command: bytes, key: RedisValueT, *args: RedisValueT, match: bool) None[source]¶
Provide feedback about a key as having either a match or drift from the actual server side value
Implementing a custom cache¶
All caches accepted by Redis or RedisCluster
must implement AbstractCache
- class AbstractCache[source]¶
Abstract class representing a local cache that can be used by
Redisorcoredis.RedisCluster- abstractmethod get(command: bytes, key: RedisValueT, *args: RedisValueT) ResponseType[source]¶
Fetch the cached response for command/key/args combination
- abstractmethod put(command: bytes, key: RedisValueT, *args: RedisValueT, value: ResponseType) None[source]¶
Cache the response for command/key/args combination
- abstractmethod invalidate(*keys: RedisValueT) None[source]¶
Invalidate any cached entries for the provided keys
- abstract property stats: CacheStats¶
Returns the current stats for the cache
- abstract property confidence: float¶
Confidence in cached values between 0 - 100. Lower values will result in the client discarding and / or validating the cached responses
- abstractmethod feedback(command: bytes, key: RedisValueT, *args: RedisValueT, match: bool) None[source]¶
Provide feedback about a key as having either a match or drift from the actual server side value
- class CacheStats(hits: Counter[bytes] = <factory>, misses: Counter[bytes] = <factory>, invalidations: Counter[bytes] = <factory>, dirty: Counter[bytes] = <factory>)[source]¶
Summary of statics to be used by instances of
AbstractCacheThe individual counters exposed are not guaranteed to retain fine grained per key metrics but the totals (returned bycoredis.patterns.cache.CacheStats.summary) will be maintained aggregated.
Internal cache wrappers¶
- class NodeTrackingCache(connection_pool: BaseConnectionPool[ConnectionT], cache: AbstractCache | None = None, max_idle_seconds: float = 15)[source]¶
Wraps an AbstractCache instance to use server assisted client caching to ensure local cache entries are invalidated if any operations are performed on the keys by another client.
- Parameters:
connection_pool¶ – Connection pool used to acquire a connection for the tracking connection
cache¶ – AbstractCache instance to wrap
max_idle_seconds¶ – Maximum duration (in seconds) to tolerate no updates from the server before performing a keepalive check with a
PING. If no updates or successful ping occur within this period, the cache is considered unhealthy (as reflected by thehealthyproperty).
- async run(task_status: TaskStatus[None] = TASK_STATUS_IGNORED) None[source]¶
Run a single connection that listens for invalidation messages, with reconnection logic.
- property confidence: float¶
Confidence in cached values between 0 - 100. Lower values will result in the client discarding and / or validating the cached responses
- feedback(command: bytes, key: RedisValueT, *args: RedisValueT, match: bool) None¶
Provide feedback about a key as having either a match or drift from the actual server side value
- get(command: bytes, key: RedisValueT, *args: RedisValueT) ResponseType¶
Fetch the cached response for command/key/args combination
- invalidate(*keys: RedisValueT) None¶
Invalidate any cached entries for the provided keys
- put(command: bytes, key: RedisValueT, *args: RedisValueT, value: ResponseType) None¶
Cache the response for command/key/args combination
- property stats: CacheStats¶
Returns the current stats for the cache
- class ClusterTrackingCache(connection_pool: BaseConnectionPool[ClusterConnection], cache: AbstractCache | None = None, max_idle_seconds: float = 15)[source]¶
An LRU cache for redis cluster that uses server assisted client caching to ensure local cache entries are invalidated if any operations are performed on the keys by another client.
The cache maintains an additional connection per node (including replicas) in the cluster to listen to invalidation events
- property confidence: float¶
Confidence in cached values between 0 - 100. Lower values will result in the client discarding and / or validating the cached responses
- feedback(command: bytes, key: RedisValueT, *args: RedisValueT, match: bool) None¶
Provide feedback about a key as having either a match or drift from the actual server side value
- get(command: bytes, key: RedisValueT, *args: RedisValueT) ResponseType¶
Fetch the cached response for command/key/args combination
- invalidate(*keys: RedisValueT) None¶
Invalidate any cached entries for the provided keys
- put(command: bytes, key: RedisValueT, *args: RedisValueT, value: ResponseType) None¶
Cache the response for command/key/args combination
- property stats: CacheStats¶
Returns the current stats for the cache