Lightweight Cache Manager for .Net

Simple cache manger came out of a need to get rid of some bulky libraries to do the simple caching of data. It’s only a couple of classes and easy to just copy-paste into your project.

Here is the API:

{% highlight c# %} var cacheManager = new CacheManager(); cacheManager.Add(“key1”, “value1”, DateTime.UtcNow, TimeSpan.FromSeconds(300)); cacheManager.Add(“key2”, “value2”, DateTime.UtcNow, TimeSpan.FromSeconds(600));

Assert.True(cacheManager.Contains(“key1”)); Assert.True(cacheManager.Contains(“key1”));

Assert.Equal(“value1”, cacheManager.GetData(“key1”)); Assert.Equal(“value1”, cacheManager.GetData(“key1”)); {% endhighlight %}

It’s as simple as that. Note the fact you need to pass the DateTime Now as a parameter? This is to make the testing easy. Maybe an overload can be used there.

Internally it is a thin wrapper around a dictionary. Full code is in my mtmk GitHub repository with the same name as the post.


March 11, 2015 | 123 Words

Ziya Suzen (Maxwell Troy)'s personal notes on software development and related technical subjects.