Skip to content

Caching

from uwsgiconf.runtime.caching import Cache

# We'll access preconfigured cache named `mycache`.
cache = Cache('mycache')

key_exists = 'mykey' in cache

def my_setter(key):
    if key == 'anotherkey':
        return 'yes'
    return 'no'

# Getting cached value and populating it if required in one pass:
yes_or_no = cache.get('anotherkey', setter=my_setter)

Classes

Cache

Cache(name: str, timeout: int | None = None)

Interface for uWSGI Caching subsystem.

Warning

To use this helper one needs to configure cache(s) in uWSGI config beforehand.

1
E.g.: ``section.caching.add_cache('mycache', 100)``

keys

.keys()

Returns a list of keys available in cache.

ValueError If cache is unavailable.


clear

.clear()

Clears cache the cache.


decr

.decr(key: str, delta: int = 1) -> bool

Decrements the specified key value by the specified value.


delete

1
2
3
.delete(
    key: str,  # (1)!
)
  1. The cache key to delete.

Deletes the given cached key from the cache.


div

.div(key: str, value: int = 2) -> bool

Divides the specified key value by the specified value.


get

1
2
3
4
5
6
7
.get(
    key: str,  # (1)!
    default: Any = None,  # (2)!
    as_int: bool = False,  # (3)!
    setter: Callable[[str],  Any] | None = None,  # (4)!
    preserve_bytes: bool = False,  # (5)!
) -> Strint | bytes
  1. The cache key to get value for.

  2. Value to return if none found in cache.

  3. Return 64bit number instead of a str. To be used with incr, decr, mul, div.

  4. Setter callable to automatically set cache value if not already cached. Required to accept a key and return a value that will be cached.

  5. If True, bytes representation is returned.

Gets a value from the cache.


incr

.incr(key: str, delta: int = 1) -> bool

Increments the specified key value by the specified value.


mul

.mul(key: str, value: int = 2) -> bool

Multiplies the specified key value by the specified value.


set

1
2
3
4
5
.set(
    key: str,  # (1)!
    value: Any,  # (2)!
    timeout: int | None = None,  # (3)!
) -> bool
  1. Cache key to set.

  2. Value to store in cache. !!! note This value will be casted to str->bytes (as uWSGI cache works with bytes-like objects).

  3. 0 not to expire. Object default is used if not set.

Sets the specified key value.