Cache Provider in One Line of Code in C#

Also available in portuguese

In this article I will share my own Cache Provider implementation in c# to use everywhere calling in one line of code.

The cache provider follows SOLID principles.

SOLID: Making an analogy, there are 5 guidelines that developers should follow in order not to duplicate code, use less dependency, use more inheritance among other things. The basic rule is to make the code easier to maintain and more scalable.

Cache Provider in One Line of Code in C#
 

The main usage:

var customer = _cacheProvider.CacheAsync(() => GetCustomer(id), id);

The cache key will be “GetCustomer” + id. The cache provider will check in cache, if exists in cache, will get from cache. If is not in cache, get from “GetCustomer” method.

var genders = _cacheProvider.CacheAsync(() => GetGenders());

The cache key will be “GetGenders”. The cache provider will check at cache, if exists in cache will get from cache. If is not in cache, get from “GetGenders” method.

var userName = _cacheProvider.CacheAsync(UseCacheOrNot(), () => GetUserName(_loggedUser), _loggedUser);

The cache key will be “GetUserName” + _loggedUser. The cache provider will check in cache only if “UseCacheOrNot” method return true, if returns true and exists in cache will get from cache. If is not in cache, get from “GetUserName” method.

The complete usage in HomeController:

HomeController constructor:

public HomeController(IAsyncCacheProvider cacheProvider)
{
    _cacheProvider = cacheProvider;
    _loggedUser = "fabio silva lima"; //sample
}

HomeController Index action:

public async Task<ActionResult> Index(string id = "0")
{
      _dateTime = DateTime.Now;

      var customer = _cacheProvider.CacheAsync(() => GetCustomer(id), id);
      var genders = _cacheProvider.CacheAsync(() => GetGenders());
      var userName = _cacheProvider.CacheAsync(UseCacheOrNot(), () => GetUserName(_loggedUser), _loggedUser);

      await Task.WhenAll(customer, genders, userName);

      var viewModel = new HomePageViewModel()
      {
           Customer = customer.Result,
           Genders = genders.Result,
           UserName = userName.Result,
           Date = _dateTime
       };

       return View(viewModel);
}

Dependency Injection:

I am using Ninject to inject the dependency for “IAsyncCacheProvider”, “ICacheProvider” in “HomeController” constructor configured in “NinjectWebCommon.cs” file.

private static void RegisterServices(IKernel kernel)
{
      kernel.Bind<ICache>().To<HttpRuntimeCache>();
      kernel.Bind<ICacheProvider>().To<FslCacheProvider>();
      kernel.Bind<IAsyncCacheProvider>().To<FslCacheProvider>();
}

Remarks:

The class “FslCacheProvider” uses “ICache” interface. It means you can write your own implementation for ICache interface. For now, I did using HttpRuntime of System.Web on HttpRuntime Cache class. “FslCacheProvider” does not know what or where the cache is stored…

Finally:

– There are others methods in IAsyncCacheProvider and ICacheProvider.
– The last parameter of all methods are array of keys, it means you can provide more than one key, for instance:

var customer = _cacheProvider.CacheAsync(() => GetCustomer(id, filter), id, filter);

The above cache key will be “GetCustomer” + id + filter.

This is it. I hope it helped you.

Cache Provider in one line of code

Cache Provider in C#: Questions, suggestions and feedbacks will be appreciated. Good luck!

Do complete download of the source code on github.

Try demo online of that application on codefinal.
About the Author:
He works as a solution architect and developer, has more than 16 years of experience in software development on several platforms and more than 14 years only for the insurance market.