/// <summary> /// 获取值 /// </summary> /// <param name="cacheKey"></param> /// <returns></returns> public object Get(string cacheKey) { var obj = cache.GetObject(cacheKey); //处理缓存 if (obj != null) { //获取过期时间 var timeSpanKey = string.Format("SessionCache_{0}", cacheKey); var timeSpan = CacheHelper.Get(timeSpanKey); if (timeSpan != null) { lock (queue) { //如果key存在,则不保存 if (!queue.Any(p => p.Key == cacheKey)) { var data = new QueueTimeSpan { Key = cacheKey, TimeSpan = (TimeSpan)timeSpan }; queue.Enqueue(data); } } } } return(obj); }
/// <summary> /// 保存Queue数据 /// </summary> /// <param name="state"></param> private void SaveCache(object state) { while (true) { if (queue.Count > 0) { QueueTimeSpan data = null; lock (queue) { data = queue.Dequeue(); } //保存值 if (data != null && cache != null) { try { cache.SetExpired(data.Key, DateTime.Now.Add(data.TimeSpan)); } catch { } } } //暂停10毫秒 Thread.Sleep(10); } }