Cache缓存的遍历与清除所有缓存

遍历缓存数据

        //取得Cache的枚举
        IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
        while (CacheEnum.MoveNext())
        {
            //缓存的Key和Value
            var cacheItem = Server.HtmlEncode(CacheEnum.Key.ToString()+" | "+CacheEnum.Value.ToString());
            Response.Write(cacheItem+"
"); }

清除所有缓存

        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));

 

转载于:https://www.cnblogs.com/taobox/archive/2012/08/31/2664948.html

你可能感兴趣的:(java)