Cache mainly to improve the data read speed, in some processing slower, high time requirements of local, try the cache or necessary
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; using System.Text; using System.Threading.Tasks; namespace SuperMaterial.Common { /// <summary> /// 缓存帮助类 /// </summary> public class CacheHelper { /// <summary> /// 默认缓存 /// </summary> private static CacheHelper Default { get { return newCacheHelper ();}} /// <Summary> /// buffering initialization /// </ Summary> Private the MemoryCache Cache = MemoryCache.Default; /// <Summary> /// lock /// </ Summary> Private Object Locker = new new Object (); /// <Summary> // constructor /// </ Summary> Private CacheHelper () { // CacheItemPolicy new new CacheItemPolicy policy = (); // create a policy cache entry /// / expiration time set, the following two set up only one //= the DateTimeOffset new new policy.AbsoluteExpiration (DateTime.Now.AddMinutes (. 5)); // set some time after the evicted cache // policy.SlidingExpiration the TimeSpan new new = (0, 0, 10); // set a a period of time not to access the evicted cache /// / eviction notice, the following two set up only one // policy.UpdateCallback = arguments the => {Console.WriteLine ( "about to cast out cache" + arguments. Key);}; } /// <Summary> /// Gets an object from the cache /// </ Summary> /// <typeParam name = "T"> Object type </ typeParam> /// <param name = "key"> key </ param> /// <Returns> cache Object </returns> public static T the Get <T> ( String Key) { return Default.GetFromCache <T> (Key); } /// <Summary> /// Gets an object from the cache /// </ Summary> /// <name typeParam = "T"> Object type </ typeParam> /// <param name = "key"> key param </> /// <Returns> cache Object </ Returns> Private T GetFromCache <T> ( String key) { Lock (Locker) { IF (Cache.Contains(key)) { return (T)cache[key]; } return default(T); } } /// <summary> /// 添加泛型缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="seconds">默认缓存180s</param> public static void Set<T>(string key, T value, double seconds = 180) { Default.SetToCache<T>(key, value, seconds); } /// <summary> /// 泛型缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="seconds">默认缓存180s</param> private void SetToCache<T>(string key, T value,= absoluteExpiration {CacheItemPolicy ()new= CacheItemPolicy policy {seconds)doubleDateTime.Now.AddSeconds (seconds The) }; Lock (Locker) { cache.Set (Key, value, Policy); } } /// <Summary> /// acquired set of keys /// </ Summary> // / <Returns> set key </ Returns> public static the ICollection < String > keys () { return Default.GetCacheKeys (); } /// <Summary> /// Gets a collection of keys /// </ Summary> / // <Returns> set bond</ Returns> Private the ICollection < String > GetCacheKeys () { Lock (Locker) { the IEnumerable <KeyValuePair < String , Object >> items = cache.AsEnumerable (); return items.Select (m => m.Key) .ToList ( ); } } /// <Summary> /// determines whether the object cache /// </ Summary> /// <param name = "key"> key </ param> /// <Returns> whether present </ returns> public static BOOL Contain ( String Key) { return Default.ContainKey (Key); } /// <Summary> /// determines whether there is the object cache /// </ Summary> /// <param name = "Key" > key </ param> /// <Returns> whether there </ Returns> Private BOOL ContainKey ( String key) { Lock (Locker) { return cache.Contains (key); } } /// <Summary> ///Data objects removed from the cache object /// </ Summary> /// <param name = "Key"> key </ param> public static BOOL the Remove ( String Key) { return Default.RemoveFromCache (Key); } / // <Summary> /// data objects removed from the cache object /// </ Summary> /// <param name = "key"> key </ param> Private BOOL RemoveFromCache ( String key) { Lock (Locker ) { IF (cache.Contains (Key)) { cache.Remove(key); return true; } return false; } } /// <summary> /// 清除实例 /// </summary> public static void Clear() { Default.ClearCache(); } /// <summary> /// 清除实例 /// </summary> private void ClearCache() { lock (locker) { cache.ToList () the ForEach (m.=>Cache.Remove (m.Key)); } } /// <Summary> /// Gets a cached object set /// </ Summary> /// <typeParam name = "T"> Cache Object Type </ typeparam> /// <Returns> cached object set </ Returns> public static the ICollection <T> Values <T> () { return Default.GetValues <T> (); } /// <Summary> /// Gets a cached object collection /// </ Summary> /// <typeParam name = "T"> cache Object type </ typeparam> <returns>/// cache object set </ Returns> Private the ICollection <T> the GetValues <T> () { Lock (Locker) { the IEnumerable <KeyValuePair < String , Object >> items = cache.AsEnumerable (); return items.Select (m => (T) m.Value) .ToList (); } } /// <Summary> /// Get the buffer size /// </ Summary> /// <Returns> cache size </ returns>publicstaticlong Size() { return Default.GetCacheSize (); } /// <Summary> /// Get the buffer size /// </ Summary> /// <Returns> Cache Size </ Returns> Private Long getCacheSize () { Lock (Locker) { return cache.GetCount (); } } } }
use
1新增缓存 CacheHelper.Set<T>(CacheKey, CacheValue, CacheTime); 2读取缓存 if(CacheHelper.Contain(CacheKey)) { CacheValue=CacheHelper.Get<T>(CacheKey) } 3重新更新缓存 if (CacheHelper.Contain(CacheKey)) { CacheHelper.Remove(CacheKey); } CacheHelper.Set<T>(CacheKey, CacheValue, CacheTime);