Skip to content

Cache

The rite.collections.cache subpackage provides LRU, LFU, and TTL cache implementations for in-memory caching with different eviction policies.

Cache Collections Module

Provides caching data structures with various eviction policies.

Classes:

  • LRUCache: Least Recently Used cache
  • LFUCache: Least Frequently Used cache
  • TTLCache: Time-To-Live cache

Classes

LFUCache(capacity: int)

LFUCache Class

A Least Frequently Used (LFU) cache with fixed capacity.

Parameters

capacity : int Maximum number of items to store in cache.

Initialize an LFU cache.


capacity: Maximum cache size.

Functions

clear() -> None

Clear all items from cache.

delete(key: Any) -> bool

Delete a key from cache.


key: The key to delete.

bool: True if key was deleted, False if not found.
get(key: Any) -> Any | None

Get a value from cache.


key: The key to look up.

Any | None: The value or None if not found.
put(key: Any, value: Any) -> None

Add or update a key-value pair in cache.


key: The key to store.
value: The value to store.

LRUCache(capacity: int)

LRUCache Class

A Least Recently Used (LRU) cache with fixed capacity.

Parameters

capacity : int Maximum number of items to store in cache.

Initialize an LRU cache.


capacity: Maximum cache size.

Functions

clear() -> None

Clear all items from cache.

delete(key: Any) -> bool

Delete a key from cache.


key: The key to delete.

bool: True if key was deleted, False if not found.
get(key: Any) -> Any | None

Get a value from cache.


key: The key to look up.

Any | None: The value or None if not found.
items() -> list[tuple[Any, Any]]

Get all key-value pairs.


list[tuple[Any, Any]]: List of (key, value) tuples.
keys() -> list[Any]

Get all keys in cache.


list[Any]: List of keys in order of use.
put(key: Any, value: Any) -> None

Add or update a key-value pair in cache.


key: The key to store.
value: The value to store.
values() -> list[Any]

Get all values in cache.


list[Any]: List of values.

TTLCache(default_ttl: float = 300.0, max_size: int | None = None)

TTLCache Class

A cache with Time-To-Live (TTL) expiration for entries.

Parameters

default_ttl : float Default time-to-live in seconds. max_size : int | None Optional maximum cache size.

Initialize a TTL cache.


default_ttl: Default expiration time in seconds.
max_size: Optional maximum cache size.

Functions

clear() -> None

Clear all items from cache.

delete(key: Any) -> bool

Delete a key from cache.


key: The key to delete.

bool: True if key was deleted, False if not found.
get(key: Any) -> Any | None

Get a value from cache if not expired.


key: The key to look up.

Any | None: The value or None if not found/expired.
get_ttl(key: Any) -> float | None

Get remaining TTL for a key.


key: The key to check.

float | None: Remaining seconds or None if not found/expired.
put(key: Any, value: Any, ttl: float | None = None) -> None

Add or update a key-value pair with TTL.


key: The key to store.
value: The value to store.
ttl: Optional TTL in seconds (uses default if not provided).

Modules

lfu_cache

LFU Cache

Least Frequently Used (LFU) cache implementation.

Classes

LFUCache(capacity: int)
LFUCache Class

A Least Frequently Used (LFU) cache with fixed capacity.

Parameters

capacity : int Maximum number of items to store in cache.

Initialize an LFU cache.


capacity: Maximum cache size.
Functions
clear() -> None

Clear all items from cache.

delete(key: Any) -> bool

Delete a key from cache.


key: The key to delete.

bool: True if key was deleted, False if not found.
get(key: Any) -> Any | None

Get a value from cache.


key: The key to look up.

Any | None: The value or None if not found.
put(key: Any, value: Any) -> None

Add or update a key-value pair in cache.


key: The key to store.
value: The value to store.

lru_cache

LRU Cache

Least Recently Used (LRU) cache implementation.

Classes

LRUCache(capacity: int)
LRUCache Class

A Least Recently Used (LRU) cache with fixed capacity.

Parameters

capacity : int Maximum number of items to store in cache.

Initialize an LRU cache.


capacity: Maximum cache size.
Functions
clear() -> None

Clear all items from cache.

delete(key: Any) -> bool

Delete a key from cache.


key: The key to delete.

bool: True if key was deleted, False if not found.
get(key: Any) -> Any | None

Get a value from cache.


key: The key to look up.

Any | None: The value or None if not found.
items() -> list[tuple[Any, Any]]

Get all key-value pairs.


list[tuple[Any, Any]]: List of (key, value) tuples.
keys() -> list[Any]

Get all keys in cache.


list[Any]: List of keys in order of use.
put(key: Any, value: Any) -> None

Add or update a key-value pair in cache.


key: The key to store.
value: The value to store.
values() -> list[Any]

Get all values in cache.


list[Any]: List of values.

ttl_cache

TTL Cache

Time-To-Live (TTL) cache with automatic expiration.

Classes

TTLCache(default_ttl: float = 300.0, max_size: int | None = None)
TTLCache Class

A cache with Time-To-Live (TTL) expiration for entries.

Parameters

default_ttl : float Default time-to-live in seconds. max_size : int | None Optional maximum cache size.

Initialize a TTL cache.


default_ttl: Default expiration time in seconds.
max_size: Optional maximum cache size.
Functions
clear() -> None

Clear all items from cache.

delete(key: Any) -> bool

Delete a key from cache.


key: The key to delete.

bool: True if key was deleted, False if not found.
get(key: Any) -> Any | None

Get a value from cache if not expired.


key: The key to look up.

Any | None: The value or None if not found/expired.
get_ttl(key: Any) -> float | None

Get remaining TTL for a key.


key: The key to check.

float | None: Remaining seconds or None if not found/expired.
put(key: Any, value: Any, ttl: float | None = None) -> None

Add or update a key-value pair with TTL.


key: The key to store.
value: The value to store.
ttl: Optional TTL in seconds (uses default if not provided).

options: show_root_heading: true show_source: false heading_level: 2