Cache#

class Cache(max_number_of_nodes, cache_size=None)#

Bases: Generic[K, V]

Least-frequently-used (LFU) cache with an optional capacity.

Parameters:
  • max_number_of_nodes (int) – Only cache keys with len(key) <= max_number_of_nodes.

  • cache_size (int | None) – Maximum number of entries to keep. If None, the cache grows without limit.

cache_size#

The capacity limit or None.

max_number_of_nodes#

Maximum allowed len(key) to be cached.

Notes

  • Eviction policy is LFU using a simple usage counter. Tie-breaking is arbitrary.

  • Updating an existing key bumps its usage counter by 1 (preserves your original behavior).

__init__(max_number_of_nodes, cache_size=None)#
Parameters:
  • max_number_of_nodes (int)

  • cache_size (int | None)

Return type:

None

Methods

__init__(max_number_of_nodes[, cache_size])

clear()

Remove all entries.

get(key)

Return the cached value for key and increment its usage, or None if missing.

set(key, value)

Insert or update key with value, enforcing LFU eviction if at capacity.

get(key)#

Return the cached value for key and increment its usage, or None if missing.

Parameters:

key (K)

Return type:

V | None

set(key, value)#

Insert or update key with value, enforcing LFU eviction if at capacity.

Keys with len(key) > max_number_of_nodes are ignored (not cached).

Parameters:
  • key (K)

  • value (V)

Return type:

None

clear()#

Remove all entries.

Return type:

None