主页 > 软件教程

ASP.NET清除全部缓存的方法及实现代码

软件教程 2024-01-09

缓存就是把常常用的数据暂存于内存中,使程序不用频繁访问数据库或硬盘。不但大大提高了程序的效率,并且减小了数据库的压力,也减少了访问硬盘的次数。缓存都是以占用内存为代价的,内存毕竟有限,不需的数据也要及时移除,但在应用中常常不是只用几个,所以管理上不是很方便,幸好有移除全部缓存的方法。

1、ASP.NET删除指定缓存(C#)

string cacheKey = "TestCache";

//添加缓存
  Cache.Add(cacheKey, "缓存内容", null, DateTime.Now.AddMinutes(30), TimeSpan.Zero, CacheItemPriority.High, null);

Cache.Remove(cacheKey);//删除缓存

2、ASP.NET清除全部缓存(C#)

IDictionaryEnumerator allCaches = HttpRuntime.Cache.GetEnumerator();

while (allCaches.MoveNext())
  {
    Cache.Remove(allCaches.Key.ToString());
  }
  Response.Write("缓存全部移除!");


标签: ASP.NET清除全部缓存

电脑软硬件教程网 Copyright © 2016-2030 www.computer26.com. Some Rights Reserved.