gpt4 book ai didi

c# - 在 HttpContext.Cache 中搜索时使用通配符

转载 作者:行者123 更新时间:2023-11-30 12:27:07 24 4
gpt4 key购买 nike

有没有办法使用通配符或正则表达式从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com