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)
-
class
uwsgiconf.runtime.caching.
Cache
(name: str, *, timeout: int = None)¶ Interface for uWSGI Caching subsystem.
Warning
To use this helper one needs to configure cache(s) in uWSGI config beforehand.
E.g.:
section.caching.add_cache('mycache', 100)
Parameters: - name – Cache name with optional address (if @-syntax is used).
- timeout –
Expire timeout (seconds). Default: 300 (5 minutes). Use 0 to not to set a timeout (not to expire).
Note
This value is ignore if cache is configured not to expire.
-
keys
¶ Returns a list of keys available in cache.
Raises: ValueError – If cache is unavailable.
-
clear
()¶ Clears cache the cache.
-
get
(key: str, *, default: Any = None, as_int: bool = False, setter: Callable = None) → Union[str, int]¶ Gets a value from the cache.
Parameters: - key – The cache key to get value for.
- default – Value to return if none found in cache.
- as_int – Return 64bit number instead of str.
- setter – Setter callable to automatically set cache value if not already cached. Required to accept a key and return a value that will be cached.
-
set
(key: str, value: Any, *, timeout: int = None) → bool¶ Sets the specified key value.
Parameters: - key –
- value –
Note
This value will be casted to string as uWSGI cache works with strings.
- timeout – 0 to not to expire. Object default is used if not set.
-
delete
(key: str)¶ Deletes the given cached key from the cache.
Parameters: key – The cache key to delete.
-
incr
(key: str, *, delta: int = 1) → bool¶ Increments the specified key value by the specified value.
Parameters: - key –
- delta –
-
decr
(key: str, *, delta: int = 1) → bool¶ Decrements the specified key value by the specified value.
Parameters: - key –
- delta –
-
mul
(key: str, *, value: int = 2) → bool¶ Multiplies the specified key value by the specified value.
Parameters: - key –
- value –
-
div
(key: str, *, value: int = 2) → bool¶ Divides the specified key value by the specified value.
Parameters: - key –
- value –