Files
Mahdi ramezaan zaadeandAlexander Medvednikov 459a8daa32 cache: add LRU and TTL caches (#30371)
* cache: add LRU and TTL caches (fixes #30317)

* cache: use monotonic TTL expiry and document cache APIs

* cache: convert generic TTL durations without alias method lookup

---------

Co-authored-by: Alexander Medvednikov <alexander@medvednikov.com>
2026-10-14 14:01:46 +03:00
..
2026-10-14 14:01:46 +03:00

cache

new_lru[K, V](capacity) creates a least-recently-used cache with a positive capacity. put inserts or replaces a value; get returns an option and promotes a hit. get_or_compute calls its factory only on a miss. contains does not change recency. delete reports whether a key was present, and len counts stored entries. Recency maintenance is linear in the number of cached entries.

new_ttl[K, V](ttl) creates a cache whose entries expire after the given duration. set restarts an entry's TTL; get removes an expired entry and returns none. purge_expired removes all expired entries and returns their count. len includes expired entries until they are accessed or purged. Expiry has millisecond resolution; a TTL below one millisecond expires immediately. The default clock is monotonic. new_ttl_with_clock[K, V](ttl, now) accepts a callback returning millisecond timestamps; custom clocks should be monotonic too.

Both caches are single-threaded. Synchronize access before sharing a cache between threads. Keys must be supported V map keys.