Cache缓存的遍历与清除所有缓存
遍历缓存数据
//取得Cache的枚举 IDictionaryEnumerator CacheEnum = Cache.GetEnumerator(); while (CacheEnum.MoveNext()) { //缓存的Key和Value var cacheItem = Server.HtmlEncode(CacheEnum.Key.ToString()+" | "+CacheEnum.Value.ToString()); Response.Write(cacheItem+"<br>"); }
清除所有缓存
List<string> keys = new List<string>(); IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator(); while (enumerator.MoveNext()) { keys.Add(enumerator.Key.ToString()); } for (int i = 0; i < keys.Count; i++) { HttpRuntime.Cache.Remove(keys[i]); }
添加固定时间缓存,超过指定的时间缓存就会被系统回收
//添加固定时间缓存 HttpRuntime.Cache.Insert("DemoKey01", DateTime.Now.ToString("HH:mm:ss"), null, DateTime.Now.AddSeconds(10), TimeSpan.Zero);
添加划动时间缓存,只要缓存在时间内被使用过,清除缓存的时间就会被重置
//添加滑动时间缓存 HttpRuntime.Cache.Insert("DemoKey02", DateTime.Now.ToString("HH:mm:ss"), null, DateTime.MaxValue, TimeSpan.FromSeconds(10));
量的积累到质的飞越