API References

All of the API references for the lruheap package are listed below.

lru.lrucache

class lru.lrucache.BoundedLRUCache

Abstract base class for bounded LRU cache implementation

abstractmethod clear_all() None

Clear all cache in element

class lru.lrucache.LRUCache(capacity: int = 128, seconds: int = 900, thread_safe: bool = False)

Initial class for representing LRUCache, given the several parameter such as :

Parameters:
  • capacity – param for set the cache capacity, maximum number is 128

  • seconds – param for set the duration for store the cache, maximum is 15 minutes

  • thread_safe – param for enable/disable thread safe option, default is False

clear_all() None

Clear all cache in element

clear_cache_key(key: int) None

Clear cache in element based on their key.

Parameters:

key – given key parameter as an integer to clear the cache

get(key: int) Any

Get the objects based on their key in cache element

Parameters:

key – given key parameter as an integer

get_cache(key: int) bool

Get cache in element based on their key, return True if the element has a key, otherwise return False when element hasn’t a key.

Parameters:

key – given key parameter as an integer to fetch the cache

get_capacity() bool

Get cache capacity, return True if the cache is full otherwiser return False when the cache is not full.

get_dict() dict

Returned a dict type in cache element.

get_duration(expired_time: int = 3600) bool

Get duration of cache, return True if the duration is exceed for expired time otherwise return False when the duration is even or below the expired time.

Parameters:

expired_time – given expired_time parameter as an integer in seconds

get_lru_element() Any

Returned a least recently used element in cache element.

get_ttl(key: int) int | bool

Get time-to-live an objects based on their cache keys. Return False if the objects hasn’t a key or time-to-live is expired.

Parameters:

key – given key parameter as an integer to fetch the TTL

is_empty() bool

Check whether the cache element is empty or not, return True if is empty otherwise will return False if is not empty

set(key: int, value: str) dict

Set an objects that wants to be cached

Parameters:
  • key – given key parameter as an integer

  • value – given value parameter of that key as an string

property ttl: int

property for get TTL (time-to-live) in seconds.

lru.heap

class lru.heap.Heap(cache_list=None)

Base class for heap object.

add(key: object, value: object) None

Add and append the element in index.

property build_floyd_heap: list | None

Build Min-Heap based on Floyd’s linear-time heap construction algorithm.

remove() None

Remove minimum element in index.

remove_key(key) list

Remove element in index based on their key.

update(key: object, value: object) list

Update key and value element in index.

lru.decorators

LRUCache module for Decorators

lru.decorators.lru_cache(capacity: int = 128, **kwargs) Any

Decorators for LRUCache classes. Given the capacity of cache based on LRUCache classes

Example:

@lru_cache(capacity=3)
def foo(x):
    pass
lru.decorators.lru_cache_time(capacity: int = 128, seconds: int = 900, **kwargs) int

Decorator that wraps a function with an LRUCache instance and a time-based expiry. The entire cache is cleared automatically once the TTL has elapsed, and the expiration window time resets from that point

Parameters:
  • (int) (seconds) – maximum number of entries to store in the cache queue

  • (int) – cache TTL in seconds before cache clear is being triggered

  • kwargs – additional keywords argument passed to the LRUCache

Returns:

Callabe: a decorator that wraps the target with time-based caching

Example

@lru_cache_time(capacity=3, seconds=180)
def foo(x):
    return x * 2

lru.utils

LRUCache module for bypass thread safe in LRU.

class lru.utils.BypassThreadSafe

Classes for bypassing thread safe in LRU Cache class.

lru.utils.generate_hash_key(*args, **kwargs) int

Helper function to generate a stable integer hash key from the given function arguments. Otherwise, when performed the cache lookup, passing arguments causing inconsistency hashing