예제 #1
0
        /// <summary>将对象放入缓存</summary>
        /// <param name="key">缓存对象的key</param>
        /// <param name="value">需要缓存的对象</param>
        /// <param name="policy">缓存的处理策略</param>
        public void SetCache(string key, object value, MemoryCachePolicy policy)
        {
            lock (lockobj)
            {
                MemoryCacheEntry cacheEntry = new MemoryCacheEntry(this, key, value, policy);
                this._cache.Add(key, cacheEntry);

                //刷新激活时间
                cacheEntry.RefreshActivityTime();
            }
        }
예제 #2
0
        /// <summary>获取缓存对象</summary>
        /// <typeparam name="T">缓存对象类型</typeparam>
        /// <param name="key">缓存对象的key</param>
        /// <returns></returns>
        public T GetCache <T>(string key)
        {
            //判断缓存是否过期
            CheckExpireAndRemove(key);

            if (_cache.ContainsKey(key))
            {
                MemoryCacheEntry cacheEntry = this._cache[key];
                //刷新激活时间
                cacheEntry.RefreshActivityTime();

                return((T)cacheEntry.Value);
            }
            else
            {
                throw new Exception(string.Format("不存在key为{0}的缓存对象", key));
            }
        }