Write in front
In the statistical module of the project, the time spent in the query is too long, by optimizing the SQL statement or adding the cache to improve the speed of the query, I have a cache of auxiliary classes, easy to manipulate the data in the cache.
Cachehelper
1 usingSystem;2 usingSystem.Collections;3 usingSystem.Collections.Generic;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 usingsystem.web;8 usingSystem.Web.Caching;9 namespaceWebSite.Statistics.Core.UtilitiesTen { One /// <summary> A ///Cache Helper Classes - /// </summary> - Public Static classCachehelper the { - /// <summary> - ///get cached data by key - /// </summary> + /// <param name= "CacheKey" ></param> - /// <returns></returns> + Public Static ObjectGetCache (stringCacheKey) A { atCache Objcache =Httpruntime.cache; - returnObjcache.get (CacheKey); - } - /// <summary> - ///setting up the cache - /// </summary> in /// <param name= "CacheKey" >Cache Key</param> - /// <param name= "ObjValue" >Cache Key</param> to Public Static voidSetcache (stringCacheKey,ObjectObjValue) + { -Cache cache =Httpruntime.cache; the Cache. Insert (CacheKey, objvalue); * } $ /// <summary>Panax Notoginseng ///setting up the cache - /// </summary> the /// <param name= "CacheKey" >Cache Key</param> + /// <param name= "ObjValue" >the cached value</param> A /// <param name= "Timeout" >Expiry Time</param> the Public Static voidSetcache (stringCacheKey,ObjectObjValue, TimeSpan timeout) + { -Cache cache =Httpruntime.cache; $Cache. Insert (CacheKey, ObjValue,NULL, DateTime.MaxValue, timeout, System.Web.Caching.CacheItemPriority.NotRemovable,NULL); $ } - /// <summary> - ///setting up the cache the /// </summary> - /// <param name= "CacheKey" >Cache Key</param>Wuyi /// <param name= "ObjValue" >the cached value</param> the /// <param name= "absoluteexpiration" >Absolute Expiration Time</param> - /// <param name= "slidingexpiration" >Sliding Expiration Time</param> Wu Public Static voidSetcache (stringCacheKey,ObjectObjValue, DateTime absoluteexpiration, TimeSpan slidingexpiration) - { AboutCache cache =Httpruntime.cache; $Cache. Insert (CacheKey, ObjValue,NULL, absoluteexpiration, slidingexpiration); - } - /// <summary> - ///removes the specified cache A /// </summary> + /// <param name= "CacheKey" ></param> the Public Static voidRemoveCache (stringCacheKey) - { $System.Web.Caching.Cache Cache =Httpruntime.cache; the Cache. Remove (CacheKey); the } the /// <summary> the ///Removing all caches - /// </summary> in Public Static voidRemoveallcache () the { theSystem.Web.Caching.Cache Cache =Httpruntime.cache; AboutIDictionaryEnumerator Cacheenum =Cache. GetEnumerator (); the while(Cacheenum.movenext ()) the { the Cache. Remove (CacheEnum.Key.ToString ()); + } - } the }Bayi}
Summarize
Cache used a lot of places, the package of this class, but also a very basic thing, is a record, after the use of time, convenient to find.
[Tool class] Cache helper Class