This project has moved and is read-only. For the latest updates, please go here.

The TinyThreadSafeCache Class



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.


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
      return _instance;

And use it anywhere :


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


jmjulien Mar 5, 2012 at 3: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 4: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.