CacheLRU

Constructors

this
this()
Undocumented in source.

Members

Functions

cacheEvents
auto cacheEvents()
clear
void clear()
enableCacheEvents
auto enableCacheEvents()
get
Nullable!V get(K k)
length
size_t length()
Undocumented in source. Be warned that the author may not have intended to support it.
put
PutResult put(K k, V v, TTL ttl)
remove
bool remove(K k)
size
auto size(size_t s)
Undocumented in source. Be warned that the author may not have intended to support it.
size
size_t size()
ttl
deprecated auto ttl(uint d)
Undocumented in source. Be warned that the author may not have intended to support it.
ttl
auto ttl(Duration d)
Undocumented in source. Be warned that the author may not have intended to support it.
ttl
Duration ttl()

Structs

CacheEventRange
struct CacheEventRange(K, V)
Undocumented in source.

Examples

import core.thread;

// very basic example
auto lru = new CacheLRU!(int, string);

lru.size(4).ttl(1.seconds);
assert(lru.size == 4);
assert(lru.ttl == 1.seconds);

assert(lru.length == 0);
lru.put(1, "one");
lru.put(2, "two");
lru.put(3, "three");
lru.put(4, "four");

auto v = lru.get(2);
assert(v=="two");
v = lru.get(4);
assert(v=="four");

assert(lru.length == 4);
// As we reached cache capacity, next `put` must evict oldest never accessed key '1'
lru.put(5, "five");
assert(lru.length == 4);    // length did not changed
assert(lru.get(1).isNull);  // really evicted

Thread.sleep(2.seconds);
v = lru.get(2); // it must be expired by ttl
assert(v.isNull);
assert(lru.length == 3);
v = lru.get(3); // it must be expired by ttl too
assert(v.isNull);
assert(lru.length == 2);
auto lru = new CacheLRU!(int, string);
() @nogc @safe nothrow {
    lru.enableCacheEvents();
    lru.put(1, "one");
    assert(lru.get(1) == "one");
    lru.put(1, "next one");
    assert(lru.get(1) == "next one");
    auto events = lru.cacheEvents();
    assert(events.length == 1); // replaced old value
    lru.put(2, "two");
    lru.clear();
    events = lru.cacheEvents();
    assert(events.length == 2); // removed keys 1 and 2 during clear()
}();

Meta