Skip to main content

CacheBackend

Cache result backend.

Attributes​

AttributeTypeDescription
serverslist = nullA list of server addresses parsed from the backend URI used to establish connections for cache operations.
supports_autoexpireboolean = trueA boolean flag indicating whether the cache backend natively supports automatic expiration of keys.
supports_native_joinboolean = trueA boolean flag indicating whether the backend supports native join operations for result coordination.
implements_incrboolean = trueA 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​

NameTypeDescription
app[Celery](../../app/base/celery.md?sid=celery_app_base_celery)The Celery application instance.
expires`intfloat
backendstr = NoneThe backend URL or name to use.
optionsdict = NoneAdditional configuration options for the cache client.
urlstr = NoneThe URL of the cache server.
**kwargsdictAdditional 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​

NameTypeDescription
keystringThe unique identifier for the cached data to be retrieved.

Returns​

TypeDescription
AnyThe 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​

NameTypeDescription
keyslistA sequence of unique identifiers for the cached data to be retrieved.

Returns​

TypeDescription
listA 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​

NameTypeDescription
keystringThe unique identifier under which the value will be stored.
valueAnyThe data to be cached.

Returns​

TypeDescription
boolTrue 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​

NameTypeDescription
keystringThe unique identifier of the cached data to be removed.

Returns​

TypeDescription
boolTrue if the key was successfully deleted.

incr()​

@classmethod
def incr(
key: string
) - > int

Atomically increments the numerical value of a key by one.

Parameters​

NameTypeDescription
keystringThe unique identifier of the numerical value to increment.

Returns​

TypeDescription
intThe 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​

NameTypeDescription
keystringThe unique identifier of the cached item to update.
valueintThe new expiration time in seconds.

Returns​

TypeDescription
boolTrue 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​

TypeDescription
objectThe 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​

TypeDescription
stringA string representation of the backend connection details in URI format.