CacheMap

A CacheMap with a key type of K and value type of V

Members

Aliases

ReplacementDelegate
alias ReplacementDelegate = V delegate(K)

A replacement function which takes in the key of type K and returns a value of type V

ReplacementFunction
alias ReplacementFunction = V function(K)

A replacement function which takes in the key of type K and returns a value of type V

Classes

CacheMap
class CacheMap

A caching map which when queried for a key which does not exist yet will call a so-called replacement function which produces a result which will be stored at that key's location

Examples

Tests the usage of the CacheMap type along with the expiration of entries mechanism

int i = 0;
int getVal(string)
{
    i++;
    return i;
}

// Create a CacheMap with 10 second expiration and 10 second sweeping interval
CacheMap!(string, int) map = new CacheMap!(string, int)(&getVal, dur!("seconds")(10));

// Get the value
int tValue = map["Tristan"];
assert(tValue == 1);

// Get the value (should still be cached)
tValue = map["Tristan"];
assert(tValue == 1);

// Wait for expiry (by sweeping thread)
Thread.sleep(dur!("seconds")(11));

// Should call replacement function
tValue = map["Tristan"];
assert(tValue == 2);

// Wait for expiry (by sweeping thread)
writeln("Sleeping now 11 secs");
Thread.sleep(dur!("seconds")(11));

// Destroy the map (such that it ends the sweeper)
destroy(map);

Creates a CacheMap which tests out the on-access expiration checking of entries by accessing an entry faster then the sweep interval and by having an expiration interval below the aforementioned interval

int i = 0;
int getVal(string)
{
    i++;
    return i;
}

// Create a CacheMap with 5 second expiration and 10 second sweeping interval
CacheMap!(string, int) map = new CacheMap!(string, int)(&getVal, dur!("seconds")(5), dur!("seconds")(10));

// Get the value
int tValue = map["Tristan"];
assert(tValue == 1);

// Wait for 5 seconds (the entry should then be expired by then for on-access check)
Thread.sleep(dur!("seconds")(5));

// Get the value (should have replacement function run)
tValue = map["Tristan"];
assert(tValue == 2);

// Destroy the map (such that it ends the sweeper
destroy(map);

Tests the usage of the CacheMap, specifically the explicit key removal method

int i = 0;
int getVal(string)
{
    i++;
    return i;
}

// Create a CacheMap with 10 second expiration and 10 second sweeping interval
CacheMap!(string, int) map = new CacheMap!(string, int)(&getVal, dur!("seconds")(10), dur!("seconds")(10));

// Get the value
int tValue = map["Tristan"];
assert(tValue == 1);

// Remove the key
assert(map.removeKey("Tristan"));

// Get the value
tValue = map["Tristan"];
assert(tValue == 2);

// Destroy the map (such that it ends the sweeper
destroy(map);

Meta