程序开发 > C# > 正文

ASP.NET清除全部缓存的方法

亮术网 2020-09-19 本网原创

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

 

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("缓存全部移除!");