TtlCache
A cache whose entries expire once ttl has elapsed since they were stored.
An entry is live while its age is strictly less than ttl; at exactly ttl it is expired. Expired entries are removed lazily: get, getOrPut, and remove treat them as absent, and purgeExpired sweeps them eagerly. size therefore includes expired entries that have not been swept yet.
When put inserts a new key while the cache holds maxSize entries, it first purges expired entries and then, if still full, evicts the least recently used live entry. get and a getOrPut hit refresh recency; replacing a key with put refreshes both recency and the entry's age.
Time is measured with the TimeSource.WithComparableMarks passed to the constructor, TimeSource.Monotonic by default.
This class is not thread-safe.
Type Parameters
the key type.
the value type.
Throws
Constructors
Creates an empty cache with the given time-to-live, capacity, and time source.
Properties
The number of stored entries, including expired entries that have not been swept by purgeExpired or touched by another operation yet.
Functions
Removes every expired entry and returns how many were removed. Runs in time linear in size.