简体   繁体   English

集中式方法来清空ASP.NET中的Cache对象

[英]Centralized way to empty the Cache object in asp.net

I have to maintain an old asp.net application, and need to disable all access to the Cache object for debugging purposes. 我必须维护一个旧的asp.net应用程序,并且需要出于调试目的而禁用对Cache对象的所有访问。 There are hundreds of pages that use the structure: 使用该结构的页面有数百个:

if (Cache["myobject"] != null)
{
   //get data from the cache
   //...
}
else
{
   //go to the DB, load the cache and send it to the client
}

I know I should refactor this code but, in the meantime, do you know of any simple and centralized way to empty (or disable) the Cache object? 我知道我应该重构此代码,但与此同时,您是否知道有任何简单且集中的方式来清空(或禁用)Cache对象?

Not sure if it will be of any help, but you can play with cache settings and set, say, privateBytesLimit to 1 and privateBytesPollTime to 00:00:01 . 不知道这是否是有帮助的,但你可以玩的缓存设置和设定,比如说, privateBytesLimit1privateBytesPollTime00:00:01 My guess is that ASP.NET should basically remove all entries as soon as they're added. 我的猜测是,ASP.NET基本上应在添加所有条目后立即将其删除。 However, this might not be the case. 但是,事实并非如此。

我没有使用过,但是此帮助程序可能会给您带来帮助

If you just want to clear the cache once, you can use this: 如果您只想清除一次缓存,则可以使用以下方法:

   foreach ( System.Collections.DictionaryEntry entry in HttpContext.Current.Cache )
        HttpContext.Current.Cache.Remove( entry.Key as String );

Otherwise @Anton Gogolev solution looks good to prevent new entries. 否则,@ Anton Gogolev解决方案看起来不错,可以防止出现新条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM