Import std.collections.lru_cache
std.collections.lru_cache
A cache of up to a set number of entries, which drops the one used least recently.
LruCache<K, V>: up to Capacity entries, dropping the one used least recently to make room for
a new key. Get and Add count as uses, but Peek, Contains, and going over the entries don't. Its
entries are a Map's, in the order they were last used, so a use moves an entry to the end.
std/collections/lru_cache.fb
Types
LruCache
class
@DebugEntries Public Class LruCache<K: Hashable, V> Implements Iterable<(K, V)>, Cloneable
A Map that holds up to its Capacity, letting go of the entry used least recently to make room for a new one.
Examples
Local cache = LruCache<String, Int>(2)
cache.Add("a", 1)
cache.Add("b", 2)
Expect(cache.Get("a") == 1)
// "b" is now the least recently used, so it goes to make room.
Expect(cache.Add("c", 3) == ("b", 2))
Expect(cache.Keys() == ["a", "c"])std/collections/lru_cache.fb, line 25
-
An empty LruCache that holds up to
capacityentries, which must be 1 or more. -
How many entries it holds.
-
How many entries it holds at most.
-
The
key's value, or Null when it isn't there. A key that's there is used. -
The
key's value, or Null when it isn't there, without using thekey. -
Whether
keyhas an entry, which doesn't count as using it. -
Adds an entry, or gives an existing
keyits new value, and uses thekey. When that leaves more than Capacity entries, the least recently used one goes, and Add gives it, or else Null. -
Removes the
key's entry, and tells whether there was one. -
Takes every entry out.
-
Holds up to
capacityentries from now on, which must be 1 or more, dropping the least recently used ones beyond it. -
The keys, least recently used first.
-
The values, in their keys' order.
-
A new LruCache with the same entries, in the same order, and Capacity.
-
Each key and its value, least recently used first. Giving a key a new value or using one while going over them panics, as adding or removing one does.
Extends
LruCache
extend
Extend LruCache<K, V> Implements Printable
Shows an LruCache in a string, as its ToString writes it.
std/collections/lru_cache.fb, line 122
-
Its entries, the least recently used first, as a Map shows them: "{a: 1, b: 2}".
Report an issue with this page or with FutureBlitz on GitHub.