Caching in ASP.NET

The main reason for low performance in the application is unwanted hits to the server. Caching helps in improving the performance and scalability of the application.

The static content/ data which will not change frequently can be kept in cache. The .Net framework enables to store data in memory for rapid access by using caching. The data will be retrieved from the server for the first time and stored in cache memory, so that when the same data is accessed again, it will be served from cache. The caching can be implemented on client and server side.

Let us see an example of implementing the server caching.

  1. public static class CacheHandler  
  2. {  
  3.     public static void Add < T > (T objInfo, string key)  
  4.     {  
  5.         HttpContext.Current.Cache.Insert(key, objInfo, null, DateTime.Now.AddMinutes(1440), System.Web.Caching.Cache.NoSlidingExpiration);  
  6.     }  
  7.     public static void Clear(string key)  
  8.     {  
  9.         HttpContext.Current.Cache.Remove(key);  
  10.     }  
  11.     public static bool Exists(string key)  
  12.     {  
  13.         return HttpContext.Current.Cache[key] != null;  
  14.     }  
  15.     public static bool Get < T > (string key, out T value)  
  16.     {  
  17.         try  
  18.         {  
  19.             if (!Exists(key))  
  20.             {  
  21.                 value =  
  22.                     default (T);  
  23.                 return false;  
  24.             }  
  25.             value = (T) HttpContext.Current.Cache[key];  
  26.         }  
  27.         catch  
  28.         {  
  29.             value =  
  30.                 default (T);  
  31.             return false;  
  32.         }  
  33.         return true;  
  34.     }  
  35. }  
The above static class has 4 static methods,

  • Add
  • Clear
  • Exists
  • Get

Add method:

  1. public static void Add < T > (T objInfo, string key)  
  2. {  
  3.     HttpContext.Current.Cache.Insert(key, objInfo, null, DateTime.Now.AddMinutes(1440), System.Web.Caching.Cache.NoSlidingExpiration);  
  4. }  
The add method gets the input of generic type and key. It will insert the Cache in the HttpContext.Current.Cache using key and time to expiry is added in minutes.

Exists Method:
  1. public static bool Exists(string key)  
  2. {  
  3.     return HttpContext.Current.Cache[key] != null;  
  4. }  
The Exists method checks whether the key is present in the current http context. This method will be used whenever we try to get the cache or to check whether any data available for the key.

Clear Method:
  1. public static void Clear(string key)  
  2. {  
  3.     HttpContext.Current.Cache.Remove(key);  
  4. }  
The clear method can removes the cache of corresponding key passed to it.

Get Method :
  1. public static bool Get < T > (string key, out T value)  
  2. {  
  3.     try  
  4.     {  
  5.         if (!Exists(key))  
  6.         {  
  7.             value =  
  8.                 default (T);  
  9.             return false;  
  10.         }  
  11.         value = (T) HttpContext.Current.Cache[key];  
  12.     }  
  13.     catch  
  14.     {  
  15.         value =  
  16.             default (T);  
  17.         return false;  
  18.     }  
  19.     return true;  
  20. }  
The get method retrieves the data from cache for the key passed to it and returns a bool of true and object of T as an output parameter is returned. If the key doesn’t exist in cache default values of object of type is returned as output parameter with false as a return value.

The above class can be used wherever the server caching is needed.

Ebook Download
View all
Learn
View all