Commonly used method is simple cache

 

Cite this

using System.Runtime.Caching;

 

 

 

        ///  <Summary> 
        /// Get the data cache
         ///  </ Summary> 
        ///  <param name = "Key"> Key key_ method name rule identification field _ </ param> 
        public  static  Object GetCache ( String key)
        {
            ObjectCache cache = MemoryCache.Default;
            var value = cache[key];
            return value;
        }

        /// <summary>
        /// 缓存值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="ExpirationTime">过期时间</param>
        public static void SetCache(string key, object value, int ExpirationTime = 6000)
        {
            if (GetCache(key) != null)
            {
                return;
            }

            ObjectCache cache = MemoryCache.Default;
            CacheItemPolicy policy = new CacheItemPolicy
            {
                AbsoluteExpiration = new new the DateTimeOffset (DateTime.Now.AddSeconds (The ExpirationTime)) // set the expiration time 
            };
            cache.Set(key, value, policy);
        }

 

Guess you like

Origin www.cnblogs.com/guxingy/p/11362765.html