有没有办法使用通配符或正则表达式从 HttpContext.Cache
中搜索和删除项目?
我的缓存中可以有“item_1”、“item_2”、...、“item_n”,我想从缓存中删除与模式“item_*”的键相关的所有值。如何在不检查项目是否存在然后将其删除的情况下实现这一目标?
例如:
代替:
HttpContext.Current.Cache.Remove("item_1")
HttpContext.Current.Cache.Remove("item_2")
HttpContext.Current.Cache.Remove("item_3")
我想要这样的东西:
HttpContext.Current.Cache.Remove("item_*")
最佳答案
你可以像这样循环项目:
foreach(var key in HttpContext.Current.Cache)
{
if (key.StartsWith("item_"))
{
// remove the corresponding item here.
}
}
基本示例,需要进行一些调整以匹配您的实现。
据我所知,您不能根据通配符删除项目,因为您需要特定的 key 。 (请证明我错了)
关于c# - 在 HttpContext.Cache 中搜索时使用通配符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26751509/