usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web.Hosting;usingSystem.Web;usingSystem.Web.Caching;usingSystem.IO;usingSystem.Runtime.Caching;namespaceSurinderBhomra.Common{publicclassCacheEngine{privatestaticreadonlyObjectCacheCache=MemoryCache.Default;/// <summary>/// Retrieve cached item./// </summary>/// <typeparam name="T">Type of cached item</typeparam>/// <param name="key">Name of cached item</param>/// <returns>Cached item as type</returns>publicstaticTGet<T>(stringkey)whereT:class{try{return(T)Cache[key];}catch{returnnull;}}/// <summary>/// Insert value into the cache using appropriate name/value pairs./// </summary>/// <typeparam name="T">Type of cached item</typeparam>/// <param name="objectToCache">Item to be cached</param>/// <param name="key">Name of item</param>/// <param name="minutes">Number of minutes for object to stay in cache</param>publicstaticvoidAdd<T>(TobjectToCache,stringkey,intminutes)whereT:class{Cache.Add(key,objectToCache,DateTime.Now.AddMinutes(minutes));}/// <summary>/// Insert value into the cache using appropriate name/value pairs./// </summary>/// <param name="objectToCache">Item to be cached</param>/// <param name="key">Name of item</param>/// <param name="minutes">Number of minutes for object to stay in cache</param>publicstaticvoidAdd(objectobjectToCache,stringkey,intminutes){Cache.Add(key,objectToCache,DateTime.Now.AddMinutes(minutes));}/// <summary>/// Remove item from cache./// </summary>/// <param name="key">Name of cached item</param>publicstaticvoidClear(stringkey){Cache.Remove(key);}/// <summary>/// Check for item in cache./// </summary>/// <param name="key">Name of cached item</param>/// <returns></returns>publicstaticboolExists(stringkey){returnCache.Get(key)!=null;}/// <summary>/// Gets all cached items as a list by their key./// </summary>/// <returns></returns>publicstaticList<string>GetAll(){returnCache.Select(keyValuePair=>keyValuePair.Key).ToList();}}}
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.