- add
bool add(const(K) key, V value)
Undocumented in source. Be warned that the author may not have intended to support it.
- contains
bool contains(const(K) key)
Undocumented in source. Be warned that the author may not have intended to support it.
- get
bool get(const(K) key, V value)
Undocumented in source. Be warned that the author may not have intended to support it.
- getOldest
const(Entry)* getOldest()
Undocumented in source. Be warned that the author may not have intended to support it.
- keys
auto keys()
keys returns a slice of the keys in the cache, from oldest to newest.
- length
uint length()
Undocumented in source. Be warned that the author may not have intended to support it.
- opIndex
V opIndex(const(K) key)
Undocumented in source. Be warned that the author may not have intended to support it.
- opIndexAssign
void opIndexAssign(V value, const(K) key)
Undocumented in source. Be warned that the author may not have intended to support it.
- opSlice
EvictList.Range!false opSlice()
Undocumented in source. Be warned that the author may not have intended to support it.
- peek
bool peek(const(K) key, V value)
Undocumented in source. Be warned that the author may not have intended to support it.
- peek
V peek(const(K) key)
Undocumented in source. Be warned that the author may not have intended to support it.
- purge
void purge()
Undocumented in source. Be warned that the author may not have intended to support it.
- remove
bool remove(const(K) key)
Undocumented in source. Be warned that the author may not have intended to support it.
- removeOldest
const(Result!(Entry*)) removeOldest()
Undocumented in source. Be warned that the author may not have intended to support it.
- setEvict
void setEvict(EvictCallback evict)
Undocumented in source. Be warned that the author may not have intended to support it.
- update
bool update(const(K) key, V value, bool upsert)
Undocumented in source. Be warned that the author may not have intended to support it.
import std.string : representation;
alias Key = immutable(ubyte[]);
static struct E {
string x;
static E undefined() {
return E("Not found");
}
}
alias TestLRU = LRU!(Key, E);
uint count;
void onEvicted(scope const(Key) key, TestLRU.Element* e) @safe {
count++;
}
auto l = new TestLRU(&onEvicted);
Key make_key(size_t num) {
return format("data %d", num).representation;
}
enum N = 4;
foreach (i; 0 .. N) {
auto key = make_key(i);
auto e = E(i.to!string);
l[key] = e;
}
assert(l[make_key(N)] == E.undefined);
assert(l[make_key(N - 1)] != E.undefined);
assert(l.length == N);
assert(l.remove(make_key(2)));
assert(count == 1);
assert(l.length == N - 1);
assert(l[make_key(2)] == E.undefined);
// alias TestSyncLRU = SyncLRU!(Key, E);
alias LRU!(int, int) TestLRU;
uint evictCounter;
void onEvicted(scope const(int) i, TestLRU.Element* e) @safe {
assert(e.entry.key == e.entry.value || e.entry.value == e.entry.key * 7);
evictCounter++;
}
bool ok;
auto l = new TestLRU(&onEvicted, 2);
int x = 1;
ok = l.add(1, x);
assert(!ok);
assert(evictCounter == 0, "should not have an eviction");
x++;
ok = l.add(2, x);
assert(!ok);
assert(evictCounter == 0, "should still not have an eviction");
ok = l.get(2,x);
assert(ok);
assert(x == 2, "check inserted");
x = 14;
ok = l.update(2,x);
assert(ok);
ok = l.get(2, x);
assert(ok);
assert(x == 14, "check updates");
x = 3;
ok = l.update(3,x,true);
assert(ok);
assert(evictCounter == 1, "should have an eviction");
ok = l.get(3, x);
assert(ok);
assert(x == 3, "check upsert");