Skip to main content

Cache.set()

Store value under key, replacing any existing value.

Usage

Source

Cache.set(
    key,
    value,
    expire=None,
)

Values of 64 KiB or more (after encoding) are stored in their own file.

Parameters

key: str

The key.

value: Any

The value: str, bytes, numbers, Decimal, bool, None, and lists, tuples, dicts, sets, frozensets, datetimes and dates of those, or anything picklable with pickle=True.

expire: float | None = None
Seconds until the entry expires, or None for never.

Returns

bool

True. With durable=True, False if the write failed and was dropped: whatever was

stored under key stays, and can be served stale.

Raises

TypeError

The value can’t be stored exactly without pickle, or expire is a bool.

Timeout
Another process held the write lock for longer than timeout.

Examples

cache.set('episodes:popular', episodes, expire=300)   # expires in 5 minutes
cache.set('config', {'retries': 3})                   # never expires