store
Store
from dara.core.internal.store import Store
class Store()
A simple key value store class that works across multiple async requests and can manage separate version of the store for the selected cache type. Every method accepts the cache_type flag that will cause it to save/fetch the key from the selected based store (session or user) rather than the global one.
Has support for a pending variable state that will allow multiple requests for the same key to asynchronously block and resolve at the same time when the value is correctly updated by the first one to request the specific key
Methods
get
def get(key: str, cache_type: Optional[CacheType] = CacheType.GLOBAL) -> Any
Get a given key from the store and optionally pull it from the specified cache store or the global one
Arguments:
key: the key to fetchcache_type: whether to pull the value from the specified cache store or the global one, defaults to the global one
get_or_wait
async def get_or_wait(key: str,
cache_type: Optional[CacheType] = CacheType.GLOBAL
) -> Any
Wait for the given key to not be pending and then return the value. Optionally pull it from the specified cache specific
store or the global one
Arguments:
key: the key to fetchcache_type: whether to pull the value from the specified cache specific store or the global one, defaults to the global one
set
def set(key: str,
value: Any,
cache_type: Optional[CacheType] = CacheType.GLOBAL,
error: Optional[Exception] = None)
Set the value of a given key in the store. the cache_type flag can be used to optionally pull the value from
the session, user or the global specific store.
Arguments:
key: the key to setvalue: the value to storecache_type: whether to pull the value from the specified cache specific store or the global one, defaults to the global oneerror: optional error; if provided, pending values will be updated with the error
set_pending_value
def set_pending_value(key: str,
cache_type: Optional[CacheType] = CacheType.GLOBAL)
Set a pending state for a value in the store. This will trigger the async behavior of the get call if subsequent
requests ask for the same key. A future is created in the store, which all requests then listen for the resolution of before returning.
Arguments:
key: the key to set as pendingcache_type: whether to pull the value from the specified cache specific store or the global one, defaults to the global one
set_pending_task
def set_pending_task(key: str,
pending_task: PendingTask,
cache_type: Optional[CacheType] = CacheType.GLOBAL)
Store a pending task state for a given key in the store. This will trigger the async behavior of the get call if subsequent
requests ask for the same key. The PendingTask will be resolved once the underlying task is completed.
Arguments:
key: the key to set as pendingpending_task: the PendingTask to storecache_type: whether to pull the value from the specified cache specific store or the global one, defaults to the global one
remove_starting_with
def remove_starting_with(start: str,
cache_type: Optional[CacheType] = CacheType.GLOBAL)
Remove any entries stored under keys starting with given string
Arguments:
start: string to use to remove entriescache_type: whether to pull the value from the specified cache specific store or the global one, defaults to the global one
empty_stores
def empty_stores(include_pending: bool = True)
Empty all of the internal stores
Arguments:
include_pending: whether to also remove pending values and tasks from store
list
def list(cache_type: Optional[CacheType] = CacheType.GLOBAL) -> List[str]
List all keys in a specified cache store. Listed the global store unless cache_type is not None
Arguments:
cache_key: whether to list the specified cache specific store or the global one, defaults to global