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

标签 c# regex sqlcachedependency

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

相关文章:

c# - 如何防止 usercontrol 表单处理 c# 中的键盘输入(箭头键)

python - 如何在python中使字符串中的字符串可选

asp.net - SqlCacheDependency/SqlDependency 和列

asp.net - 扩展 ASP.NET 数据缓存以在网络场中共享

c# - 如何自动生成免费接收和存储服务的构造函数?

c# - JQuery GridView 控件

c# - Xamarin : @(Content) build action is not supported

javascript - 在正则表达式中使用变量的简单方法

c++ - 使用具有特殊字符的正则表达式标记 C++ 字符串

asp.net-core - .Net 核心 SQL 缓存依赖