simplecache.png

A Simple Cache

Notice that there isn't a generic cache available to use in .NET?  There are specific ones hanging out in places, such as the System.Web.Caching namespace for instance, but they are for pretty focused purposes.  Thus, occasionally it has been necessary for me to throw together a caching object for my many nefarious purposes in coding. 

 

Opposite is the VS2k5 diagram image of the object for you to have a quick glance at it.  Usage is pretty simple as the object is simply another Dictionary. 

 

All entries (unless you want of course) are subject to expiry.  If they are not used and expire, they get removed by a timer that fires every few seconds.  If the entries do get accessed, then expiry slides accordingly by a time you can set (if of course you want the expiry to slide). 

 

SimpleCache is also totally threadsafe (unlike the standard Generic collections).

 

Here it is for your programming pleasure.  Enjoy.

 

NB: Oh, Code Analysis produces this:

"CA1710:IdentifiersShouldHaveCorrectSuffix"

you can either suppress it like I do or rename the object to have the 'Dictionary' suffix as the rule suggests.