Cache
Peak Cache Client.
Provides caching functionality with tenant-based key prefixing. Supports cache operations with JSON serialization and connection management.
- class peak.resources.cache.CacheClient(session=None, *, debug_logs=True, additional_prefix=None, connection_config=None)
Peak Cache Client for caching operations.
Provides auto key prefixing based on tenant names to ensure proper isolation and access control patterns.
Inherits from BaseClient to use the default session pattern.
- Parameters:
session (Session) –
debug_logs (bool) –
additional_prefix (Optional[str]) –
connection_config (Optional[Dict[str, Any]]) –
- close()
Close the cache connection.
- Return type:
None
- delete(*keys)
Delete one or more keys from the cache.
- Parameters:
keys (str) – Keys to delete
- Returns:
Number of keys deleted
- Return type:
int
- Raises:
CacheError – If the operation fails
- exists(*keys)
Check if one or more keys exist in the cache.
- Parameters:
keys (str) – Keys to check
- Returns:
Number of keys that exist
- Return type:
int
- Raises:
CacheError – If the operation fails
- expire(key, ttl)
Set expiration time for a key.
- Parameters:
key (str) – The key to set expiration for
ttl (int) – Time to live in seconds
- Returns:
True if successful, False if key doesn’t exist
- Return type:
bool
- Raises:
CacheError – If the operation fails
- flush_by_pattern(pattern)
Flush keys matching a pattern within the tenant namespace.
- Parameters:
pattern (str) – Pattern to match (will be prefixed with tenant name)
- Returns:
Number of keys deleted
- Return type:
int
- Raises:
CacheError – If the operation fails
- flush_tenant()
Flush all keys for the current tenant.
- Returns:
Number of keys deleted
- Return type:
int
- Raises:
CacheError – If the operation fails
- get(key, default=None)
Get a value from the cache.
- Parameters:
key (str) – The key to get
default (Any) – Default value if key doesn’t exist
- Returns:
The value (JSON deserialized if applicable) or default
- Return type:
Any
- Raises:
CacheError – If the operation fails
- get_additional_prefix()
Get current additional prefix.
- Returns:
Current additional prefix
- Return type:
Optional[str]
- mget(*keys)
Get multiple values from the cache.
- Parameters:
keys (str) – Keys to get
- Returns:
List of values (None for missing keys)
- Return type:
List[Any]
- Raises:
CacheError – If the operation fails
- mset(mapping, ttl=None)
Set multiple key-value pairs in the cache.
- Parameters:
mapping (Dict[str, Any]) – Dictionary of key-value pairs to set
ttl (int | None) – Time to live in seconds (optional, applies to all keys)
- Returns:
True if successful
- Return type:
bool
- Raises:
CacheError – If the operation fails
- ping()
Test the connection to the cache.
- Returns:
True if connection is successful
- Return type:
bool
- Raises:
CacheError – If the connection fails
- set(key, value, ttl=None)
Set a key-value pair in the cache.
- Parameters:
key (str) – The key to set
value (Any) – The value to set (will be JSON serialized if not string)
ttl (int | None) – Time to live in seconds (optional)
- Returns:
True if successful, False otherwise
- Return type:
bool
- Raises:
CacheError – If the operation fails
- set_additional_prefix(additional_prefix)
Set additional prefix for cache keys.
- Parameters:
additional_prefix (str | None) – Additional prefix to add after tenant name
- Return type:
None
- ttl(key)
Get the time to live for a key.
- Parameters:
key (str) – The key to check
- Returns:
TTL in seconds (-1 if no expiration, -2 if key doesn’t exist)
- Return type:
int
- Raises:
CacheError – If the operation fails
- exception peak.resources.cache.CacheConnectionError
Exception for cache connection issues.
- exception peak.resources.cache.CacheError
Base exception for cache operations.
- peak.resources.cache.get_client(session=None, *, debug_logs=True, additional_prefix=None, connection_config=None)
Get a cache client instance.
- Parameters:
session (Session | None) – Peak session for authentication (optional)
debug_logs (bool) – Enable or disable debug logging (default: True)
additional_prefix (str | None) – Additional prefix to add after tenant name (optional)
connection_config (Dict[str, Any] | None) – Custom connection configuration overrides (optional)
- Returns:
Cache client instance
- Return type:
- class peak.resources.cache.CacheClient(session=None, *, debug_logs=True, additional_prefix=None, connection_config=None)
Peak Cache Client for caching operations.
Provides auto key prefixing based on tenant names to ensure proper isolation and access control patterns.
Inherits from BaseClient to use the default session pattern.
- Parameters:
session (Session) –
debug_logs (bool) –
additional_prefix (Optional[str]) –
connection_config (Optional[Dict[str, Any]]) –
- close()
Close the cache connection.
- Return type:
None
- delete(*keys)
Delete one or more keys from the cache.
- Parameters:
keys (str) – Keys to delete
- Returns:
Number of keys deleted
- Return type:
int
- Raises:
CacheError – If the operation fails
- exists(*keys)
Check if one or more keys exist in the cache.
- Parameters:
keys (str) – Keys to check
- Returns:
Number of keys that exist
- Return type:
int
- Raises:
CacheError – If the operation fails
- expire(key, ttl)
Set expiration time for a key.
- Parameters:
key (str) – The key to set expiration for
ttl (int) – Time to live in seconds
- Returns:
True if successful, False if key doesn’t exist
- Return type:
bool
- Raises:
CacheError – If the operation fails
- flush_by_pattern(pattern)
Flush keys matching a pattern within the tenant namespace.
- Parameters:
pattern (str) – Pattern to match (will be prefixed with tenant name)
- Returns:
Number of keys deleted
- Return type:
int
- Raises:
CacheError – If the operation fails
- flush_tenant()
Flush all keys for the current tenant.
- Returns:
Number of keys deleted
- Return type:
int
- Raises:
CacheError – If the operation fails
- get(key, default=None)
Get a value from the cache.
- Parameters:
key (str) – The key to get
default (Any) – Default value if key doesn’t exist
- Returns:
The value (JSON deserialized if applicable) or default
- Return type:
Any
- Raises:
CacheError – If the operation fails
- get_additional_prefix()
Get current additional prefix.
- Returns:
Current additional prefix
- Return type:
Optional[str]
- mget(*keys)
Get multiple values from the cache.
- Parameters:
keys (str) – Keys to get
- Returns:
List of values (None for missing keys)
- Return type:
List[Any]
- Raises:
CacheError – If the operation fails
- mset(mapping, ttl=None)
Set multiple key-value pairs in the cache.
- Parameters:
mapping (Dict[str, Any]) – Dictionary of key-value pairs to set
ttl (int | None) – Time to live in seconds (optional, applies to all keys)
- Returns:
True if successful
- Return type:
bool
- Raises:
CacheError – If the operation fails
- ping()
Test the connection to the cache.
- Returns:
True if connection is successful
- Return type:
bool
- Raises:
CacheError – If the connection fails
- set(key, value, ttl=None)
Set a key-value pair in the cache.
- Parameters:
key (str) – The key to set
value (Any) – The value to set (will be JSON serialized if not string)
ttl (int | None) – Time to live in seconds (optional)
- Returns:
True if successful, False otherwise
- Return type:
bool
- Raises:
CacheError – If the operation fails
- set_additional_prefix(additional_prefix)
Set additional prefix for cache keys.
- Parameters:
additional_prefix (str | None) – Additional prefix to add after tenant name
- Return type:
None
- ttl(key)
Get the time to live for a key.
- Parameters:
key (str) – The key to check
- Returns:
TTL in seconds (-1 if no expiration, -2 if key doesn’t exist)
- Return type:
int
- Raises:
CacheError – If the operation fails
- class peak.resources.cache.CacheError
Base exception for cache operations.
- class peak.resources.cache.CacheConnectionError
Exception for cache connection issues.