lru_eviction_queue

  • Declaration

    struct LRUEvictionQueue(T);

    A LRU Eviction Queue for the D programming language

    License

    Boost Software License - Version 1.0

    Examples

    1. import lru_eviction_queue; import std.stdio : stdout; // Create a queue that will hold 3 items auto cache = LRUEvictionQueue!int(3); // Fire this event when an item is evicted cache.on_evict_cb = delegate(string key, int value) { stdout.writefln("Evicted item: %s", key); }; // Fire this event when an item is updated cache.on_update_cb = delegate(string key, 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!"); }

    • Declaration

      this(ulong max_length);

      Creates a LRUEvictionQueue

      Parameters

      ulong 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

      1. auto cache = LRUEvictionQueue!string(100);

    • Declaration

      bool hasKey(string key);

      Returns true if a key is still on the queue

      Parameters

      string key

      The name of the key to check.

      Examples

      1. auto cache = LRUEvictionQueue!string(100); bool retval = cache.hasKey("name");

    • set

      Declaration

      void set(string key, T value);

      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 existing key.

      Parameters

      string key

      The key to set.

      T value

      The value to set.

      Examples

      1. auto cache = LRUEvictionQueue!string(100); cache.set("name", "Bob");

    • get

      Declaration

      T get(string key, T default_value);

      Gets the value in the queue

      Parameters

      string key

      The key to get.

      T default_value

      The value returned if the key is not found.

      Examples

      1. auto cache = LRUEvictionQueue!string(100); string name = cache.get("name", string.init);

    • Declaration

      void remove(string key);

      Removes the key from the queue. Does nothing if the key is not in the queue. Will not fire any events.

      Parameters

      string key

      The key to remove.

      Examples

      1. auto cache = LRUEvictionQueue!string(100); cache["name"] = "Alice"; cache.remove("name");

    • Declaration

      void clear();

      Removes all the items from the queue. Will not fire any events.

      Examples

      1. auto cache = LRUEvictionQueue!string(100); cache["name"] = "Tim"; cache.clear();

    • Declaration

      ulong length();

      Returns the length of the queue.

      Examples

      1. auto cache = LRUEvictionQueue!string(100); cache["name"] = "Alice"; ulong len = cache.length;

    • Declaration

      ulong max_length();

      Returns the max length of the queue before items are removed.

      Examples

      1. auto cache = LRUEvictionQueue!string(100); ulong max_len = cache.max_length;

    • Declaration

      string[] keys();

      Returns all the keys in the queue.

      Examples

      1. auto cache = LRUEvictionQueue!int(100); cache["bbb"] = 5; cache["zzz"] = 7; string[] keys = cache.keys();

    • Declaration

      int opApply(scope int delegate(ref string key, T value) dg);

      Used to iterate over the queue.

      Examples

      1. auto cache = LRUEvictionQueue!int(100); cache["bbb"] = 5; cache["zzz"] = 7; foreach (key, value ; cache) { }

    • Declaration

      T opIndex(string key);

      Gets the value in the queue. Throws if the key is not found.

      Parameters

      string key

      The key to get.

      Throws

      If the key is not found, it will throw an Exception.

      Examples

      1. auto cache = LRUEvictionQueue!string(100); cache["name"] = "Frank"; string name = cache["name"];

    • Declaration

      T opIndexAssign(T value, string key);

      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 existing key.

      Parameters

      string key

      The key to set.

      T value

      The value to set.

      Examples

      1. auto cache = LRUEvictionQueue!string(100); cache["name"] = "Lisa";

    • Declaration

      void delegate(string key, T value) on_evict_cb;

      The event to fire when an existing key is evicted

      Parameters

      on_evict_cb

      The callback to fire.

      Examples

      1. auto cache = LRUEvictionQueue!string(2); cache.on_evict_cb = delegate(string key, string value) { }; cache["aaa"] = "Lisa"; cache["bbb"] = "Sally"; cache["ccc"] = "Kevin";

    • Declaration

      void delegate(string key, T value) on_update_cb;

      The event to fire when an existing key is updated

      Parameters

      on_update_cb

      The callback to fire.

      Examples

      1. auto cache = LRUEvictionQueue!string(100); cache.on_update_cb = delegate(string key, string value) { }; cache["name"] = "Lisa"; cache["name"] = "Sally";