betty.cache package - Betty documentation

Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# betty.cache package

## Submodules

- betty.cache.file module
  - `BinaryFileCache`
    - `BinaryFileCache.cache_item_file_path()`
    - `BinaryFileCache.path`
  - `PickledFileCache`
- betty.cache.memory module
  - `MemoryCache`
    - `MemoryCache.__init__()`
- betty.cache.no_op module
  - `NoOpCache`
    - `NoOpCache.clear()`
    - `NoOpCache.delete()`
    - `NoOpCache.get()`
    - `NoOpCache.getset()`
    - `NoOpCache.set()`
    - `NoOpCache.with_scope()`

## Module contents

Provide the Cache API.

class betty.cache.Cache

  Bases: `Generic`[`_CacheItemValueContraT`], `ABC`

  Provide a cache.

  Implementations MUST be thread-safe.

  To test your own subclasses, use `betty.test_utils.cache.CacheTestBase`.

  abstract async clear() → None

    Clear all items from the cache.

  abstract async delete(cache_item_id: str) → None

    Delete the cache item with the given ID.

  abstract get(cache_item_id: str) → AsyncContextManager[CacheItem[_CacheItemValueContraT] | None]

    Get the cache item with the given ID.

  abstract getset(cache_item_id: str) → AsyncContextManager[tuple[CacheItem[_CacheItemValueContraT] | None, Callable[[_CacheItemValueContraT], Awaitable[None]]]]
  abstract getset(cache_item_id: str, *, wait: Literal[False] = False) → AsyncContextManager[tuple[CacheItem[_CacheItemValueContraT] | None, Callable[[_CacheItemValueContraT], Awaitable[None]] | None]]

    Get the cache item with the given ID, and provide a setter to add or update it within the same atomic operation.

    If `wait` is `False` and no lock can be acquired, return `None, None`. Otherwise return: 0. A cache item if one could be found, or else `None`. 1. An asynchronous setter that takes the cache item’s value as its only argument.

  abstract async set(cache_item_id: str, value: _CacheItemValueContraT, *, modified: int | float | None = None) → None

    Add or update a cache item.

  abstract with_scope(scope: str) → Self

    Return a new nested cache with the given scope.

class betty.cache.CacheItem

  Bases: `Generic`[`_CacheItemValueCoT`], `ABC`

  A cache item.

  abstract property modified: int | float

    Get the time this cache item was last modified, in seconds.

  abstract async value() → _CacheItemValueCoT

    Get this cache item’s value.