Пример #1
0
        /// <summary>
        /// 设置内存缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="secondDelay">缓存秒钟</param>
        /// <param name="data">不支持null</param>
        /// <param name="resetDelyIfExist"></param>
        public static void Set(string cacheKey, double secondDelay, object data, bool resetDelyIfExist = true)
        {
            if (data == null)
            {
                return;
            }
            gcSchedule();

            //如果正在垃圾回收,请等待
            while (isGcing)
            {
                Thread.Sleep(5);
            }

            if (_cacheDictionary.ContainsKey(cacheKey))
            {
                CacheUnit cache = (CacheUnit)_cacheDictionary[cacheKey];
                if (resetDelyIfExist)
                {
                    cache.ExpireTime = DateTime.Now.AddSeconds(secondDelay);
                }
                cache.Data = data;
            }
            else
            {
                CacheUnit cache = new CacheUnit()
                {
                    Data       = data,
                    ExpireTime = DateTime.Now.AddSeconds(secondDelay)
                };
                _cacheDictionary[cacheKey] = cache;
            }
        }
Пример #2
0
        /// <summary>
        /// 获取内存缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public static object Get(string cacheKey)
        {
            //如果正在垃圾回收,请等待
            while (isGcing)
            {
                Thread.Sleep(10);
            }

            if (!_cacheDictionary.ContainsKey(cacheKey))
            {
                return(null);
            }

            CacheUnit unit = (CacheUnit)_cacheDictionary[cacheKey];

            if (unit != null && unit.IsEnabled())
            {
                return(unit.Data);
            }

            _cacheDictionary.Remove(cacheKey);
            return(null);
        }