This project has moved. For the latest updates, please go here.

The TinyThreadSafeCache Class

ClassDiagram1.png

Methods

Add : This method will replace existing item or add if it does not already exist.
Get : This is to get an item from the cache by its key, it will return null if not found.
Delete : This method is to remove an item from the cache by its key
Purge : This method empty the whole cache.
Exist : This method validates that a key exists in the cache.
Size : This methods gets the number of items saved in the cache.

Usage

You can instanciate the class normally :

TinyThreadSafeCache<MyTypeToCache> MyCache = new TinyThreadSafeCache<MyTypeToCache>();

Or if like me you prefer the accessibility of a singleton, you can derive it :

sealed class MySingletonCache 
{
  //singleton private instance
  private static readonly TinyThreadSafeCache<MyTypeToCache> _instance = 
    new TinyThreadSafeCache<MyTypeToCache>(); 
  
  private MySingletonCache() { } // Make sure nobody can call constructor

  public static TinyThreadSafeCache<MyTypeToCache> Instance
  {
    get
    {
      return _instance;
    }
  }
}

And use it anywhere :

MySingletonCache.Instance.Purge();

Last edited Mar 5, 2012 at 3:12 AM by jmjulien, version 5

Comments

jmjulien Mar 5, 2012 at 2:52 AM 
Thank you for pointing that out. I did not think about that but it makes sense. I will update the documentation and the sample code to reflect that.

silic0re Feb 27, 2012 at 3:38 AM 
Probably not the best way to implement a singleton. You don't want to derive the singleton from your base class because it doesn't add anything. If you generate documentation for the class, the functions will be there (inherited) on MySingletonCache (such as Purge, Add, etc), but cannot be used because the constructor is private. You probably just want to make the _instance variable of type TinyThreadSafeCache<T>. That way the only member of your class is the Instance property, and doesn't have the unusable inherited properties and methods from TinyThreadSafeCache.