tlc.service.tlc_lrucache#

Module Contents#

Classes#

Class

Description

LRUCacheBackendConfig

LRUCache backend configuration.

LRUCacheBackend

In-memory LRU cache backend.

LRUCache

LRU cache where you can control how many slots are available, maximum memory to use for the cache, and a cache time out for the items.

Functions#

Function

Description

LRUFuncCache

Decorator to add an LRU cache to a function.

Data#

Data

Description

LRU_STATS_KEY

API#

tlc.service.tlc_lrucache.LRU_STATS_KEY = __stats__#
class tlc.service.tlc_lrucache.LRUCacheBackendConfig#

Bases: pydantic.BaseModel

LRUCache backend configuration.

max_entries: int = 50000#

Available slots

max_memory_in_bytes: int = 1073741824#

Maximum memory to use for the cache

time_out_in_seconds: float = 3600#

Cache time out for the items

class tlc.service.tlc_lrucache.LRUCacheBackend(config: tlc.service.tlc_lrucache.LRUCacheBackendConfig)#

Bases: starlite.cache.base.CacheBackendProtocol

In-memory LRU cache backend.

Initialize LRUCacheBackend

async get(key: str) Any#
async set(key: str, value: Any, expiration: int) None#
async delete(key: str) None#
async delete_all() None#
tlc.service.tlc_lrucache.LRUFuncCache(max_entries: int, max_memory_in_bytes: int, time_threshold_in_seconds: float, time_out_in_seconds: float = 0.0) Callable#

Decorator to add an LRU cache to a function.

The decorator can control the number of cache slots (max_entries) and how much memory to use for cached element (max_memory).

In addition, the decorator can set how long a function execution must take before the result is cached (time_threshold), to avoid caching results that are fast to compute or retrieve, thus only using the cache for slower items.

The time_out parameter can be used to set how long each cached item should remain valid. If set to 0, the items will never expire.

class tlc.service.tlc_lrucache.LRUCache(max_entries: int, max_memory: int, time_out: float = 0.0)#

LRU cache where you can control how many slots are available, maximum memory to use for the cache, and a cache time out for the items.

The stats() method will return a dictionary of important statistics about the cache. The clear() method will clear the cache and reset all statistics.

LRUEntry = None#
clear() None#
get(key: Any) Any#
set(key: Any, value: bytes) None#
delete(key: Any) None#
remove_oldest_item() None#
stats() Dict[str, int]#