lru_eviction_queue
-
Declaration
struct
LRUEvictionQueue
(KEY, VALUE);A LRU Eviction Queue for the D programming language
Discussion
Version
2.0.0
License
Boost Software License - Version 1.0
Examples
import lru_eviction_queue; import std.stdio : stdout; // Create a queue that will hold 3 items auto cache = LRUEvictionQueue!(string, int)(3); // Fire this event when an item is evicted cache.setOnEvictCb(delegate(string key, ref int value) { stdout.writefln("Evicted item: %s", key); }); // Fire this event when an item is updated cache.setOnUpdateCb(delegate(string key, ref int value) { stdout.writefln("Updated item: %s", key); }); // Add cache["aaa"] = 65; cache["bbb"] = 97; cache["ccc"] = 15; cache["ddd"] = 46; // Check that the key "aaa" was removed if (! cache.hasKey("aaa")) { stdout.writefln("Item \"aaa\" was evicted!"); } // Check that the key "ccc" was found if ("ccc" in cache) { stdout.writefln("Item \"ccc\" was found!"); } // Prints all the items in the cache foreach (key, ref value ; cache) { stdout.writefln("%s : %s", key, value); }
-
Declaration
this(size_t
max_length
);Creates a LRUEvictionQueue
Parameters
size_t
max_length
The max size of the queue. After this many items are added, the oldest items will be removed.
Throws
If
max_length
is less than 1, it will throw an Exception.Examples
auto cache = LRUEvictionQueue!(string, string)(100);
-
Declaration
bool
hasKey
(KEYkey
);Returns
true
if akey
is still on the queueParameters
KEY
key
The name of the
key
to check.Examples
auto cache = LRUEvictionQueue!(string, string)(100); bool retval = cache.hasKey("name");
-
Declaration
void
set
(KEYkey
, VALUEvalue
);Sets the
value
in the queue. Will fire the on_evict_cb event if it is a new item that pushes an item off the queue. Will fire the on_update_cb event if updating an already existingkey
.Parameters
KEY
key
The
key
toset
.VALUE
value
The
value
toset
.Examples
auto cache = LRUEvictionQueue!(string, string)(100); cache.set("name", "Bob");
-
Declaration
VALUE
get
(KEYkey
, VALUEdefault_value
);Gets the value in the queue
Parameters
KEY
key
The
key
toget
.VALUE
default_value
The value returned if the
key
is not found.Examples
auto cache = LRUEvictionQueue!(string, string)(100); string name = cache.get("name", string.init);
-
Declaration
void
remove
(KEYkey
);Removes the
key
from the queue. Does nothing if thekey
is not in the queue. Will not fire any events.Parameters
KEY
key
The
key
toremove
.Examples
auto cache = LRUEvictionQueue!(string, string)(100); cache["name"] = "Alice"; cache.remove("name");
-
Declaration
void
clear
();Removes all the items from the queue. Will not fire any events.
Examples
auto cache = LRUEvictionQueue!(string, string)(100); cache["name"] = "Tim"; cache.clear();
-
Declaration
size_t
length
();Returns the
length
of the queue.Examples
auto cache = LRUEvictionQueue!(string, string)(100); cache["name"] = "Alice"; size_t len = cache.length;
-
Declaration
size_t
max_length
();Returns the max length of the queue before items are removed.
Examples
auto cache = LRUEvictionQueue!(string, string)(100); size_t max_len = cache.max_length;
-
Declaration
KEY[]
keys
();Returns all the
keys
in the queue.Examples
auto cache = LRUEvictionQueue!(string, int)(100); cache["bbb"] = 5; cache["zzz"] = 7; string[] keys = cache.keys();
-
Declaration
int
opApply
(scope int delegate(ref KEY key, ref VALUE value)dg
);Used to iterate over the queue.
Examples
auto cache = LRUEvictionQueue!(string, int)(100); cache["bbb"] = 5; cache["zzz"] = 7; foreach (key, ref value ; cache) { }
-
Declaration
VALUE
opIndex
(KEYkey
);Gets the value in the queue. Throws if the
key
is not found.Parameters
KEY
key
The
key
to get.Throws
If the
key
is not found, it will throw an Exception.Examples
auto cache = LRUEvictionQueue!(string, string)(100); cache["name"] = "Frank"; string name = cache["name"];
-
Declaration
VALUE
opIndexAssign
(VALUEvalue
, KEYkey
);Sets the
value
in the queue. Will fire the on_evict_cb event if it is a new item that pushes an item off the queue. Will fire the on_update_cb event if updating an already existingkey
.Parameters
KEY
key
The
key
to set.VALUE
value
The
value
to set.Examples
auto cache = LRUEvictionQueue!(string, string)(100); cache["name"] = "Lisa";
-
Declaration
VALUE*
opBinary
(string op)(KEYrhs
);Returns a pointer to the value in the queue, or
null
.Parameters
KEY
rhs
The name of the key to check.
Examples
auto cache = LRUEvictionQueue!(string, string)(100); cache["name"] = "Lisa"; string* result = "name" in cache;
-
Declaration
void
setOnEvictCb
(void delegate(KEY key, ref VALUE value)cb
);The event to fire when an existing key is evicted
Parameters
void delegate(KEY key, ref VALUE value)
cb
The callback to fire.
Examples
auto cache = LRUEvictionQueue!(string, string)(2); cache.setOnEvictCb(delegate(string key, ref string value) { }); cache["aaa"] = "Lisa"; cache["bbb"] = "Sally"; cache["ccc"] = "Kevin";
-
Declaration
void
setOnUpdateCb
(void delegate(KEY key, ref VALUE value)cb
);The event to fire when an existing key is updated
Parameters
void delegate(KEY key, ref VALUE value)
cb
The callback to fire.
Examples
auto cache = LRUEvictionQueue!(string, string)(100); cache.setOnUpdateCb(delegate(string key, ref string value) { }); cache["name"] = "Lisa"; cache["name"] = "Sally";