core.cache
Lightweight Least Recently Used (LRU) cache used by core components.
This module provides a minimal LRU cache implementation backed by collections.OrderedDict. It is intended for small, fast caches such as operation-result caching inside FuzznumStrategy instances.
- class axisfuzzy.core.cache.LruCache(maxsize=None)[source]
Bases:
objectSimple Least Recently Used (LRU) cache.
The cache stores mapping from keys to values and evicts the least recently used item when capacity is exceeded.
- Parameters:
maxsize (int, optional) – Maximum number of items to hold in the cache. If
None, the value fromaxisfuzzy.config.get_config().CACHE_SIZEis used.
- maxsize
Configured maximum number of entries.
- Type:
int
- _cache
Internal mapping preserving access/insertion order.
- Type:
collections.OrderedDict
Notes
Keys must be hashable.
getreturnsNonefor missing keys (no exception raised).
Examples
>>> from axisfuzzy.core.cache import LruCache >>> c = LruCache(maxsize=2) >>> c.put('a', 1) >>> c.put('b', 2) >>> c.get('a') 1 >>> c.put('c', 3) # least recently used ('b') evicted if 'a' was accessed >>> 'b' in c False
- get(key)[source]
Retrieve a value and mark the key as recently used.
- Parameters:
key (str) – Key to lookup in the cache.
- Returns:
The stored value if the key exists, otherwise
None.- Return type:
Any or None
Examples
>>> c = LruCache(maxsize=2) >>> c.put('x', 10) >>> c.get('x') 10 >>> c.get('missing') is None True
- put(key, value)[source]
Insert or update a key-value pair in the cache.
If the key already exists, it is updated and moved to the most-recently-used position. If adding a new item causes the cache to exceed
maxsize, the least recently used item is evicted.- Parameters:
key (str) – Cache key to insert or update.
value (Any) – Value to associate with the key.
- Raises:
ValueError – If
maxsizeis configured as a non-positive integer (depends on config).- Return type:
None
Examples
>>> c = LruCache(maxsize=1) >>> c.put('a', 1) >>> c.put('a', 2) # update existing key >>> c.get('a') 2