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()
}();