3

I have to maintain an old asp.net application, and need to disable all access to the Cache object for debugging purposes. 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?

3 Answers 3

3

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. My guess is that ASP.NET should basically remove all entries as soon as they're added. However, this might not be the case.

Sign up to request clarification or add additional context in comments.

1 Comment

It works. Thanks. I don't think it's a good solution for production environments (my app now fires BeginRequest events all the time), but it's perfect testing.
3

I haven't used it but this helper might give you what you're after.

1 Comment

Looks great. It's what I should do when I come around to re-engineering the application.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.