CacheBackend
Cache result backend.
Attributes
| Attribute | Type | Description |
|---|---|---|
| servers | list = null | A list of server addresses parsed from the backend URI used to establish connections for cache operations. |
| supports_autoexpire | boolean = true | A boolean flag indicating whether the cache backend natively supports automatic expiration of keys. |
| supports_native_join | boolean = true | A boolean flag indicating whether the backend supports native join operations for result coordination. |
| implements_incr | boolean = true | A boolean flag indicating whether the backend provides a native atomic increment operation. |
Constructor
Signature
def CacheBackend(
app: [Celery](../../app/base/celery.md?sid=celery_app_base_celery),
expires: int|float|timedelta = None,
backend: str = None,
options: dict = None,
url: str = None,
**kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| app | [Celery](../../app/base/celery.md?sid=celery_app_base_celery) | The Celery application instance. |
| expires | `int | float |
| backend | str = None | The backend URL or name to use. |
| options | dict = None | Additional configuration options for the cache client. |
| url | str = None | The URL of the cache server. |
| **kwargs | dict | Additional keyword arguments passed to the parent class. |
Methods
get()
@classmethod
def get(
key: string
) - > Any
Retrieves the value associated with a specific key from the cache backend.
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | The unique identifier for the cached data to be retrieved. |
Returns
| Type | Description |
|---|---|
Any | The stored data associated with the key, or None if the key does not exist. |
mget()
@classmethod
def mget(
keys: list
) - > list
Fetches multiple values from the cache in a single operation to reduce network round-trips.
Parameters
| Name | Type | Description |
|---|---|---|
| keys | list | A sequence of unique identifiers for the cached data to be retrieved. |
Returns
| Type | Description |
|---|---|
list | A list of values corresponding to the requested keys. |
set()
@classmethod
def set(
key: string,
value: Any
) - > bool
Stores a value in the cache associated with a key, using the default expiration time configured for the backend.
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | The unique identifier under which the value will be stored. |
| value | Any | The data to be cached. |
Returns
| Type | Description |
|---|---|
bool | True if the value was successfully stored, otherwise False. |
delete()
@classmethod
def delete(
key: string
) - > bool
Removes a specific key and its associated value from the cache backend.
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | The unique identifier of the cached data to be removed. |
Returns
| Type | Description |
|---|---|
bool | True if the key was successfully deleted. |
incr()
@classmethod
def incr(
key: string
) - > int
Atomically increments the numerical value of a key by one.
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | The unique identifier of the numerical value to increment. |
Returns
| Type | Description |
|---|---|
int | The new value of the key after the increment operation. |
expire()
@classmethod
def expire(
key: string,
value: int
) - > bool
Updates the expiration time or 'touches' a key to extend its TTL in the cache.
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | The unique identifier of the cached item to update. |
| value | int | The new expiration time in seconds. |
Returns
| Type | Description |
|---|---|
bool | True if the expiration was successfully updated. |
client()
@classmethod
def client() - > object
Lazily instantiates and returns the underlying cache client using the configured servers and options.
Returns
| Type | Description |
|---|---|
object | The driver-specific cache client instance (e.g., Redis or Memcached client). |
as_uri()
@classmethod
def as_uri() - > string
Return the backend as an URI. This properly handles the case of multiple servers.
Returns
| Type | Description |
|---|---|
string | A string representation of the backend connection details in URI format. |