Skip to main content

ttl

Node

from dara.core.internal.cache_store.ttl import Node

class Node()

A node to hold the value, expiration time, and pin status of each cache entry.

Methods

__init__

def __init__(value: Any, expiration_time: float, pin: bool = False)

Initialize a new node.

Arguments:

  • value: The value to be stored.
  • expiration_time: The time at which the value expires.
  • pin: Whether the entry should be preserved even if its TTL has expired.

TTLCache

from dara.core.internal.cache_store.ttl import TTLCache

class TTLCache(CacheStoreImpl[TTLCachePolicy])

A Time-to-Live (TTL) Cache implementation that evicts entries after a specified duration.

This cache utilizes a heap data structure to efficiently track the expiration times of the cache entries. The heap ensures that the soonest-to-expire entry is always at the top, enabling quick eviction of expired entries. On each set or get operation, the cache checks and evicts expired entries based on the TTL policy. Pinned entries are not evicted until they are unpinned, regardless of their TTL.

Methods

get

async def get(key: str, unpin: bool = False) -> Any

Retrieve a value from the cache.

Arguments:

  • key: The key of the value to retrieve.
  • unpin: If true, the entry will be unpinned if it is pinned.

Returns:

The value associated with the key, or None if the key is not in the cache.

set

async def set(key: str, value: Any, pin: bool = False) -> None

Add a key-value pair to the cache, or update the value of an existing key.

Arguments:

  • key: The key to set.
  • value: The value to associate with the key.
  • pin: If true, the entry will not be evicted until read.

delete

async def delete(key: str) -> None

Delete a key-value pair from the cache, if it exists.

Arguments:

  • key: The key to delete.

clear

async def clear()

Empty the store.