1.缓存的机制就是静态字典
2. 使用缓存的时候,都是固定的套路 给一个key,然后判断是否存在,存在怎么样 不存在怎么样
3.过期问题,先上一个被动过期的例子,就是用户先查,查到过期就删除
1 private static Dictionary<string, KeyValuePair<DateTime, object>> _CustomerCacheDictionary = new Dictionary<string, KeyValuePair<DateTime, object>>(); 2 3 static CustomerCache() 4 { 5 Task.Run(() => 6 { 7 while (true) 8 { 9 if (_CustomerCacheDictionary.Count > 0) 10 { 11 List<string> list = new List<string>(); 12 foreach (var item in _CustomerCacheDictionary) 13 { 14 KeyValuePair<DateTime, object> keyValuePair = item.Value; 15 if (DateTime.Now > keyValuePair.Key)//过期了 16 { 17 list.Add(item.Key); 18 } 19 } 20 foreach (var key in list) 21 { 22 _CustomerCacheDictionary.Remove(key); 23 } 24 } 25 Thread.Sleep(1000); 26 } 27 }); 28 } 29 30 /// <summary> 31 /// 32 /// </summary> 33 /// <param name="key"></param> 34 /// <param name="data"></param> 35 /// <param name="cacheTime">默认30分钟 单位分钟</param> 36 public void Add(string key, object data, int cacheTime = 30) 37 { 38 if (_CustomerCacheDictionary.ContainsKey(key)) 39 throw new Exception("相同的key"); 40 _CustomerCacheDictionary.Add(key, new KeyValuePair<DateTime, object>(DateTime.Now.AddMinutes(cacheTime), data)); 41 } 42 43 /// <summary> 44 /// 应该先检查,再get 45 /// </summary> 46 /// <typeparam name="T"></typeparam> 47 /// <param name="key"></param> 48 /// <returns></returns> 49 public T Get<T>(string key) 50 { 51 return (T)_CustomerCacheDictionary[key].Value; 52 //if (this.Contains(key)) 53 //{ 54 // return (T)_CustomerCacheDictionary[key].Value; 55 //} 56 //else 57 //{ 58 // //return default(T);//T int 0 59 // throw new Exception("not such key"); 60 //} 61 62 //if (_CustomerCacheDictionary.ContainsKey(key)) 63 //{ 64 // KeyValuePair<DateTime, object> keyValuePair = _CustomerCacheDictionary[key]; 65 // if (DateTime.Now > keyValuePair.Key)//过期了 66 // { 67 // _CustomerCacheDictionary.Remove(key); 68 // return default(T); 69 // } 70 // else 71 // { 72 // return (T)keyValuePair.Value; 73 // } 74 //} 75 //else 76 //{ 77 // return default(T); 78 //} 79 } 80 /// <summary> 81 /// 82 /// </summary> 83 /// <param name="key"></param> 84 /// <returns></returns> 85 public bool Contains(string key) 86 { 87 //完成清理 不能用 88 //return _CustomerCacheDictionary.ContainsKey(key) && DateTime.Now <= _CustomerCacheDictionary[key].Key; 89 90 if (_CustomerCacheDictionary.ContainsKey(key)) 91 { 92 KeyValuePair<DateTime, object> keyValuePair = _CustomerCacheDictionary[key]; 93 if (DateTime.Now > keyValuePair.Key)//过期了 94 { 95 _CustomerCacheDictionary.Remove(key); 96 return false; 97 } 98 else 99 return true; 100 } 101 return false; 102 }
4.主动过期,利用异步线程技术实时查询缓存的过期时间
1 static CustomerCache() 2 { 3 Task.Run(() => 4 { 5 while (true) 6 { 7 if (_CustomerCacheDictionary.Count > 0) 8 { 9 List<string> list = new List<string>(); 10 foreach (var item in _CustomerCacheDictionary) 11 { 12 KeyValuePair<DateTime, object> keyValuePair = item.Value; 13 if (DateTime.Now > keyValuePair.Key)//过期了 14 { 15 list.Add(item.Key); 16 } 17 } 18 foreach (var key in list) 19 { 20 _CustomerCacheDictionary.Remove(key); 21 } 22 } 23 Thread.Sleep(1000); 24 } 25 }); 26 }
5.实际的项目运用中主动过期和被动过期是结合使用的,就想是上面的例子,先自动判断主动过期,如果没有检查出来,那么被动过期也可以检查出来
本文详细介绍了缓存机制的实现方式,包括静态字典作为缓存的基础,以及主动和被动过期策略的结合使用。通过具体代码示例,展示了如何在C#中实现缓存,并处理缓存项的过期问题。
1225

被折叠的 条评论
为什么被折叠?



