CoroutineMemoryCache

This is a collection of utilities to help the development

Available as Nuget package

It is based, for the internal structure, on the ConcurrencyHelpers package.

The usage is pretty simple.

Basic Usage

First the memory cache should be initialized. Note that the default duration of items in cache is 1 day and the refresh of items in cache happens every 5 seconds.

All these values can be changed through the constructor parameters.

    //Create the cache.
    //Default duration of items in cache is 1 day
    //Default cache refresh rate is every 5 seconds
    var mc = new CoroutineMemoryCache();
    //Start the cache
    mc.Start();

Then items can be added to the cache. Note that here we are only adding. It will happen in the shortest possible time.

    CoroutineMemoryCache mc;
    ...
    //With a duration
    var duration = new Timespan(0,0,10);
    mc.AddValue("key","value",duration);

    //...or with the default one
    mc.AddValue("key","value");

Then it would be possible to retrieve the data. Note the "foreach" cycle. This is needed because the current thread should wait untile the coroutine controlling the itme add process executes and terminate.

    CoroutineMemoryCache mc;
    ...
    CacheItem foundedItem = null;

    //Wait until the request to get the value is processed
    foreach(var cacheItem in mc.AddOrGetValue("key")){
        if(cacheItem!=null){
            foundedItem = cacheItem;
        }
    }

    //Check if the item exists
    if(!foundedItem.IsEmpty){
        //And use it
        Console.WriteLine(foundedItem.Data.ToString());
    }

Optionally it is possible to change (and add, if not present) the content of the cache:

    CoroutineMemoryCache mc;
    ...
    mc.AddValue("key","value");
    ...
    mc.AddOrReplaceValue("key","newValue");

Areas

The cache can be subdivided in Areas, these are qualified by a string identifier CASE INSENSITIVE and an optional duration. If not specified will be used the duration of the whole cache.

The areas must be created as soon as possible

    var mc = new CoroutineMemoryCache();

    //With a duration
    var duration = new Timespan(0,0,10);
    mc.CreateArea("tenSecondsArea",duration);

    //Or with the default one
    mc.CreateArea("defaultDurationArea");

    //Start the cache
    mc.Start();

To use the the areas the first parameter after the default ones specified is the area name, CASE INSENSITIVE.

    CoroutineMemoryCache mc;
    mc.CreateArea("areaName");
    ...
    mc.AddValue("key","value","areaName");
    ...
    mc.AddOrReplaceValue("key","newValue","areaName");
    ...
    //Note that here the value is, by purpose, null!!
    foreach(var cacheItem in mc.AddOrGetValue("key",null,"areaName")){
    ...

Refreshing the data only if not present

All the previous functions suppose that the data is already known to the caller, to pass it to the memory cache.

But suppose that the data comes from a call to a long running service. For example a wrapper on a web service call. With, for example, this interface:

public interface IMyService
{
    string RetrieveWebServiceData();
}

For our scopes we want

We can then specify during the calls, the function that will be used to retrieve the data instead of the "values".

    CoroutineMemoryCache mc;
    IMyService svc;
    ...
    var duration = new Timespan(0,0,10);
    mc.Add("key",()=>RetrieveData(svc),duration);
    ...
    mc.AddValue("key",()=>RetrieveData(svc));
    ...
    mc.AddOrReplace("key",()=>RetrieveData(svc),"areaName");
    ...
    foreach(var cacheItem in mc.AddOrGetValue("key",()=>RetrieveData(svc),"areaName")){
    ...

The only requirement for the function is that it should return an IEnumerable of object.

    IEnumerable<object> RetrieveData()
    {
        var result = RetrieveWebServiceData();
        yield return result;
    }

Licensing

Copyright (C) 2013-2014 Kendar.org

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Last modified on: April 28, 2014