Implementing Cache Provider In One Line Of Code

The main goal is to use a Cache Provider and call Cache method by writing just one line of code. Cache Provider follows SOLID principles, so you can implement the interfaces for a different solution.
 
The main usage is given below.
  1. var customer = _cacheProvider.CacheAsync(() => GetCustomer(id), id);  


You can see, I called cache method in one line of code. 
 
The cache key will be "GetCustomer" + "_" + id. For instance: "GetCustomer_100".
Cache Provider will check in cache, and if it exists in cache  it will get it from cache. If it is not in cache, get it from "GetCustomer" method.
  1. var genders = _cacheProvider.CacheAsync(() => GetGenders());  
The cache key will be just "GetGenders" because it was not provided by any cache key. 
Cache Provider will check at cache, and if it exists in cache, it will get  it from cache. If it is not in cache, get  it from "GetGenders" method.
  1. var userName = _cacheProvider.CacheAsync(UseCacheOrNot(), () => GetUserName(_loggedUser), _loggedUser);  
The cache key will be "GetUserName" + "_" + _loggedUser. For instance: "GetUserName_fabiosilvalima".
Cache Provider will check in cache only if "UseCacheOrNot" method returns true, if it returns true and exists in cache  it will get it  from cache. If it is not in cache, get it from "GetUserName" method.
 
The complete example usage in HomeController 

HomeController constructor is given below.

  1. public HomeController(IAsyncCacheProvider cacheProvider)  
  2. {  
  3.     _cacheProvider = cacheProvider;  
  4.     _loggedUser = "fabio silva lima"//sample  
  5. }  
HomeController Index action is given below.
  1. public async Task<ActionResult> Index(string id = "0")  
  2. {  
  3.       _dateTime = DateTime.Now;  
  4.   
  5.       var customer = _cacheProvider.CacheAsync(() => GetCustomer(id), id);  
  6.       var genders = _cacheProvider.CacheAsync(() => GetGenders());  
  7.       var userName = _cacheProvider.CacheAsync(UseCacheOrNot(), () => GetUserName(_loggedUser), _loggedUser);  
  8.   
  9.       await Task.WhenAll(customer, genders, userName);  
  10.   
  11.       var viewModel = new HomePageViewModel()  
  12.       {  
  13.            Customer = customer.Result,  
  14.            Genders = genders.Result,  
  15.            UserName = userName.Result,  
  16.            Date = _dateTime  
  17.        };  
  18.   
  19.        return View(viewModel);  
  20. }  

In the code shown above, I used the async methods of Cache Provider.

Dependency Injection: 
 
I am using Ninject to inject the dependency for "IAsyncCacheProvider" in "HomeController" constructor. I configured DI in "NinjectWebCommon.cs" file. You can also inject "ICacheProvider" for synchronous operations, if you want.
  1. private static void RegisterServices(IKernel kernel)  
  2. {  
  3.       kernel.Bind<ICache>().To<HttpRuntimeCache>();  
  4.       kernel.Bind<ICacheProvider>().To<FslCacheProvider>();  
  5.       kernel.Bind<IAsyncCacheProvider>().To<FslCacheProvider>();  
  6. }  

Remarks:  

The class "FslCacheProvider" uses "ICache" interface. It means you can write your own implementation for ICache interface. For this article, I used "HttpRuntime" of "System.Web" namespace and I implemented "ICache" in "HttpRuntimeCache" class.
 
The SOLID principle here is "FslCacheProvider" does not know what or where the cache is stored.

There are other methods in "IAsyncCacheProvider" and "ICacheProvider".

The last parameter of all the methods are the array of keys and it means you can provide more than one key. For instance
  1. var customer = _cacheProvider.CacheAsync(() => GetCustomer(id, filter), id, filter);  
The cache key given above will be "GetCustomer" + "_" + id + "_" + filter. For instance: "GetCustomer_100_john"
 
Yes, that's it. You can download the full source code and view an online demo by clicking the links given below.
 
Good luck. 
 
Download full source code


Similar Articles