This implements a new module named container/lru which provides two efficient and full-featured generic least recently used (LRU) data structures with additional support for optional configurable per-item expiration timeouts via a time to live (TTL) mechanism.
As compared to the existing module that this intends to replace, this new implementation makes use of generics introduced in Go 1.18 in order to provide full type safety and avoid forced allocations that the previous implementation based on interfaces required.
Both implementations are safe for use in multi-threaded (concurrent) workloads and exhibit nearly early O(1) lookups, inserts, and deletions with no additional heap allocations beyond the stored items.
As is one of the defining characteristics for LRU data structures, both are limited to a configurable maximum number of items with eviction for the least recently used entry when the limit is exceeded.
One of the new data structures is named Set and is tailored towards use cases that involve storing a distinct collection of items with existence testing. The other one is named Map and is aimed at use cases that require caching and retrieving values by key.
Both implementations support optional default TTLs for item expiration as well as provide the option to override the default TTL on a per-item basis.
An efficient lazy removal scheme is used such that expired items are periodically removed when items are added or updated. This approach allows for efficient amortized removal of expired items without the need for additional background tasks, timers or heap allocations.
The following shows the performance of the new LRU map and set:
This implements a new module named
container/lru
which provides two efficient and full-featured generic least recently used (LRU) data structures with additional support for optional configurable per-item expiration timeouts via a time to live (TTL) mechanism.As compared to the existing module that this intends to replace, this new implementation makes use of generics introduced in Go 1.18 in order to provide full type safety and avoid forced allocations that the previous implementation based on interfaces required.
Both implementations are safe for use in multi-threaded (concurrent) workloads and exhibit nearly early O(1) lookups, inserts, and deletions with no additional heap allocations beyond the stored items.
As is one of the defining characteristics for LRU data structures, both are limited to a configurable maximum number of items with eviction for the least recently used entry when the limit is exceeded.
One of the new data structures is named
Set
and is tailored towards use cases that involve storing a distinct collection of items with existence testing. The other one is namedMap
and is aimed at use cases that require caching and retrieving values by key.Both implementations support optional default TTLs for item expiration as well as provide the option to override the default TTL on a per-item basis.
An efficient lazy removal scheme is used such that expired items are periodically removed when items are added or updated. This approach allows for efficient amortized removal of expired items without the need for additional background tasks, timers or heap allocations.
The following shows the performance of the new LRU map and set:
The following shows the performance versus the old interface-based LRU for the overlapping functionality (
KVCache
->Map
,Cache
->Set
):