buildgrid.server.utils.async_lru_cache module

class buildgrid.server.utils.async_lru_cache.LruCache(max_length: int)

Bases: Generic[T]

Class implementing an async-safe LRU cache with an asynchronous API.

This class provides LRU functionality similar to the existing LruCache class, but providing an asynchronous API and using asynchronous locks internally. This allows it to be used in asyncio coroutines without blocking the event loop.

This class also supports setting a TTL on cache entries. Cleanup of entries which have outlived their TTL is done lazily when LruCache.get is called for the relevant key.

max_size() int

Get the maximum number of items that can be stored in the cache.

Calling LruCache.set when there are already this many entries in the cache will cause the oldest entry to be dropped.

Returns:

The maximum number of items to store.

Return type:

int

async size() int

Get the current number of items in the cache.

Returns:

The number of items currently stored.

Return type:

int

async get(key: str) T | None

Get the value for a given key, or None.

This method returns the value for a given key. If the key isn’t in the cache, or the value’s TTL has expired, then this returns None instead. In the case of a TTL expiry, the key is removed from the cache to save space.

Parameters:

key (str) – The key to get the corresponding value for.

Returns:

A value mapped to the given key, or None

async set(key: str, value: T, ttl: int) None

Set the value and TTL of a key.

This method sets the value and TTL of a key. A TTL of 0 or lower means that the key won’t expire due to age. Keys with a TTL of 0 or lower will still be dropped when they’re the least-recently-used key.

Parameters:
  • key (str) – The key to update.

  • value (T) – The value to map to the key.

  • ttl (int) – A TTL (in seconds) for the key.