我正在尝试使用 HttpRuntime.Cache.Remove(key) 删除缓存但无效。我想知道使用 HttpRuntime.Cache 的最佳实践是什么。
问候
我正在尝试使用 HttpRuntime.Cache.Remove(key) 删除缓存但无效。我想知道使用 HttpRuntime.Cache 的最佳实践是什么。
问候
Remove方法工作得非常好,并根据其键从缓存中删除该项目。这是一个例子:
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
}
}
这可能是你使用它的方式是错误的。不幸的是,您的问题中没有具体说明这一点,因此我们可以提供帮助。
我曾经花了一个充满乐趣的小时来寻找看起来非常相似的东西:我从缓存中删除了一些东西,却又发现它又放回了那里。原来是一个删除触发器,每次都把它放回去。寻找这样的副作用。