4

I am trying to remove the cache using the HttpRuntime.Cache.Remove(key) but invain. I wonder what are the best practices for using HttpRuntime.Cache.

Regards

code master
  • 2,026
  • 5
  • 30
  • 49
  • 3
    Please provide some more details of what exactly you're trying to do and what is/isn't happening - preferably with some example code that demonstrates the problem. – LukeH Nov 19 '10 at 17:39

2 Answers2

13

The Remove method works perfectly fine and removes the item from the cache given its key. Here's an example:

class Program
{
    static void Main()
    {
        // add an item to the cache
        HttpRuntime.Cache["foo"] = "bar";
        Console.WriteLine(HttpRuntime.Cache["foo"]); // prints bar

        // remove the item from the cache
        HttpRuntime.Cache.Remove("foo");
        Console.WriteLine(HttpRuntime.Cache["foo"]); // prints empty string
    }
}

It's probably the way you are using it that is wrong. Unfortunately this hasn't been specified in your question so that's as far as we can help.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

I once spent a fun-filled hour tracking down something that looked very similar: I removed something from cache only to find it back in there again. Turned out to be a remove-trigger that put it back each time. Look for side effects like that.

n8wrl
  • 19,439
  • 4
  • 63
  • 103
  • This^. Remember, after HttpRuntime.Cache.Remove() has been called, the delegate 'CacheItemRemoved' callback method will fire, and I would guess you're re-adding the item inside there. – maxp Jan 21 '13 at 13:21