duck.html.components.utils.cachingยถ

Module for caching components.

Module Contentsยถ

Functionsยถ

CachedComponent

This is a nested function which you can use directly with component classes without the need of using cached_component decorator.

cached_component

Decorator for returning cached components.

make_cache_key

Returns a cache key.

Dataยถ

DEFAULT_CACHE_BACKEND

APIยถ

duck.html.components.utils.caching.CachedComponent(component_cls: Type, **cache_options)[source]ยถ

This is a nested function which you can use directly with component classes without the need of using cached_component decorator.

Example:

cached_btn_func = CachedComponent(
    Button,
    namespace="welcome-btn",
    targets=["id", "text"],
) # Returns function for retrieving or creating the button instance.

btn = cached_btn_func(id="my-btn", text="Hello world")
btn.is_from_cache() # Returns False for the first time and True otherwise
btn.is_a_copy() # Returns True by default.
exception duck.html.components.utils.caching.ComponentCachingError[source]ยถ

Bases: Exception

Raised upon component caching error.

Initialization

Initialize self. See help(type(self)) for accurate signature.

duck.html.components.utils.caching.DEFAULT_CACHE_BACKENDยถ

โ€˜InMemoryCache(โ€ฆ)โ€™

duck.html.components.utils.caching.cached_component(targets: Optional[List[Union[str, Callable]]] = None, cache_backend: Optional[Any] = None, expiry: Optional[float] = None, namespace: Optional[Union[str, Callable]] = None, ignore_args: Optional[List[int]] = None, ignore_kwargs: Optional[List[str]] = None, skip_cache_attr: str = 'skip_cache', on_cache_result: Optional[Callable] = None, freeze: bool = False, return_copy: bool = True, _no_caching: bool = False)[source]ยถ

Decorator for returning cached components.

Create a new instance or return a cached instance.

Parameters:
  • targets โ€“

    These are component keyword arguments to focus on when caching or a custom caching callable which returns cache value. Example

    targets = ["some_keyword_arg", <custom_callable>]
    

  • cache_backend โ€“ A custom cache backend which implements methods get and set. If None, default cache backend is used.

  • expiry โ€“ The expiry for cache keys. Defaults to None.

  • namespace โ€“ Optional string or callable returning a namespace prefix for keys. Use namespace for grouping and easy bulky cache invalidation.

  • ignore_args โ€“ Indices to ignore in arguments. For example to ignore first argument when caching, [0] may be parsed as ignore_args.

  • ignore_kwargs โ€“ Keyword arguments keys to ignore when caching.

  • skip_cache_attr โ€“ Optional component attribute to skip caching (for debugging). This defaults to skip_cache, meaning, if component.skip_cache=True then, cache is skipped for that component.

  • on_cache_result โ€“ This is a callable that can be executed upon receiving a result from cache. If some data needs to be reinitialized, you can do this here.

duck.html.components.utils.caching.make_cache_key(component_cls, args: Any, kwargs: Dict[str, Any], namespace: Optional[str] = None) โ†’ Tuple[source]ยถ

Returns a cache key.