Caching¶
coredis.cache
Built in caches¶
- class TrackingCache(max_keys: int = 2**12, max_size_bytes: int = 64 * 1024 * 1024, max_idle_seconds: int = 5, confidence: float = 100.0, dynamic_confidence: bool = False, cache: LRUCache[LRUCache[LRUCache[ResponseType]]] | None = None, stats: CacheStats | None = None)[source]¶
An LRU cache that uses server assisted client caching to ensure local cache entries are invalidated if any operations are performed on the keys by another client.
This class proxies to either
NodeTrackingCache
orClusterTrackingCache
depending on which type of client it is passed into.- Parameters:
max_keys¶ – maximum keys to cache. A negative value represents and unbounded cache.
max_size_bytes¶ – maximum size in bytes for the local cache. A negative value represents an unbounded cache.
max_idle_seconds¶ – maximum duration to tolerate no updates from the server. When the duration is exceeded the connection and cache will be reset.
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.
- async initialize(client: 'coredis.client.Redis[Any]' | 'coredis.client.RedisCluster[Any]') TrackingCache [source]¶
Associate and initialize this cache with the provided client
- property confidence: float¶
Confidence in cached values between 0 - 100. Lower values will result in the client discarding and / or validating the cached responses
- property stats: CacheStats¶
Returns the current stats for the cache
- get_client_id(connection: BaseConnection) int | None [source]¶
If the cache supports receiving invalidation events from the server return the
client_id
that theconnection
should send redirects to.
- get(command: bytes, key: bytes, *args: ValueT) ResponseType [source]¶
Fetch the cached response for command/key/args combination
- put(command: bytes, key: bytes, *args: ValueT, value: ResponseType) None [source]¶
Cache the response for command/key/args combination
- feedback(command: bytes, key: bytes, *args: ValueT, match: bool) None [source]¶
Provide feedback about a key as having either a match or drift from the actual server side value
Create a copy of this cache that can be used to share memory with another client.
In the example below
c1
andc2
have their own instances ofTrackingCache
but share the same in-memory local cached responses:c1 = await coredis.Redis(cache=TrackingCache()) c2 = await coredis.Redis(cache=c1.cache.share())
- class NodeTrackingCache(max_keys: int = 2**12, max_size_bytes: int = 64 * 1024 * 1024, max_idle_seconds: int = 5, confidence: float = 100, dynamic_confidence: bool = False, cache: LRUCache[LRUCache[LRUCache[ResponseType]]] | None = None, stats: CacheStats | None = None)[source]¶
An LRU cache that uses server assisted client caching to ensure local cache entries are invalidated if any operations are performed on the keys by another client.
- Parameters:
max_keys¶ – maximum keys to cache. A negative value represents and unbounded cache.
max_size_bytes¶ – maximum size in bytes for the local cache. A negative value represents an unbounded cache.
max_idle_seconds¶ – maximum duration to tolerate no updates from the server. When the duration is exceeded the connection and cache will be reset.
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.
- property confidence: float¶
Confidence in cached values between 0 - 100. Lower values will result in the client discarding and / or validating the cached responses
- property stats: CacheStats¶
Returns the current stats for the cache
- get(command: bytes, key: bytes, *args: ValueT) ResponseType [source]¶
Fetch the cached response for command/key/args combination
- put(command: bytes, key: bytes, *args: ValueT, value: ResponseType) None [source]¶
Cache the response for command/key/args combination
- feedback(command: bytes, key: bytes, *args: ValueT, match: bool) None [source]¶
Provide feedback about a key as having either a match or drift from the actual server side value
- async initialize(client: 'coredis.client.Redis[Any]' | 'coredis.client.RedisCluster[Any]') NodeTrackingCache [source]¶
Associate and initialize this cache with the provided client
- get_client_id(client: BaseConnection) int | None [source]¶
If the cache supports receiving invalidation events from the server return the
client_id
that theconnection
should send redirects to.
- class ClusterTrackingCache(max_keys: int = 2**12, max_size_bytes: int = 64 * 1024 * 1024, max_idle_seconds: int = 5, confidence: float = 100, dynamic_confidence: bool = False, cache: LRUCache[LRUCache[LRUCache[ResponseType]]] | None = None, stats: CacheStats | None = None)[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
- Parameters:
max_keys¶ – maximum keys to cache. A negative value represents and unbounded cache.
max_size_bytes¶ – maximum size in bytes for the local cache. A negative value represents an unbounded cache.
max_idle_seconds¶ – maximum duration to tolerate no updates from the server. When the duration is exceeded the connection and cache will be reset.
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.
- async initialize(client: 'coredis.client.Redis[Any]' | 'coredis.client.RedisCluster[Any]') ClusterTrackingCache [source]¶
Associate and initialize this cache with the provided client
- property confidence: float¶
Confidence in cached values between 0 - 100. Lower values will result in the client discarding and / or validating the cached responses
- property stats: CacheStats¶
Returns the current stats for the cache
- get_client_id(connection: BaseConnection) int | None [source]¶
If the cache supports receiving invalidation events from the server return the
client_id
that theconnection
should send redirects to.
- get(command: bytes, key: bytes, *args: ValueT) ResponseType [source]¶
Fetch the cached response for command/key/args combination
- put(command: bytes, key: bytes, *args: ValueT, value: ResponseType) None [source]¶
Cache the response for command/key/args combination
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
coredis.Redis
orcoredis.RedisCluster
- abstract async initialize(client: 'coredis.client.Redis[Any]' | 'coredis.client.RedisCluster[Any]') AbstractCache [source]¶
Associate and initialize this cache with the provided client
- abstract get(command: bytes, key: bytes, *args: ValueT) ResponseType [source]¶
Fetch the cached response for command/key/args combination
- abstract put(command: bytes, key: bytes, *args: ValueT, value: ResponseType) None [source]¶
Cache the response for command/key/args combination
Additionally, caches can opt in to additional features by implementing any of the following protocols:
- class SupportsClientTracking(*args, **kwargs)[source]¶
If a cache implements
SupportsClientTracking
, theRedis
andRedisCluster
clients will ensure that the client returned byget_client_id()
is set using theclient_tracking()
command on any connection returned by the clients.- abstract get_client_id(connection: BaseConnection) int | None [source]¶
If the cache supports receiving invalidation events from the server return the
client_id
that theconnection
should send redirects to.
- class SupportsStats(*args, **kwargs)[source]¶
Protocol of a cache that provides cache statistics
- abstract property stats: CacheStats¶
Returns the current stats for the cache
- class SupportsSampling(*args, **kwargs)[source]¶
If a cache implements
SupportsSampling
, methods that support caching will sample the response from the cache and test it against an uncached response from the server based on the confidence returned byconfidence
. The outcome of the validation will be fed back to the cache usingfeedback()
and in the case that there was no match, the uncached response will be returned.
- class CacheStats(hits: ~collections.Counter[bytes] = <factory>, misses: ~collections.Counter[bytes] = <factory>, invalidations: ~collections.Counter[bytes] = <factory>, dirty: ~collections.Counter[bytes] = <factory>)[source]¶
Summary of statics to be used by instances of
coredis.cache.AbstractCache
The individual counters exposed are not guaranteed to retain fine grained per key metrics but the totals (returned bycoredis.cache.CacheStats.summary
) will be maintained aggregated.