循环访问 ASP.NET 缓存对象中的键

发布于 2024-10-05 03:39:30 字数 890 浏览 1 评论 0原文

ASP.NET 中的缓存看起来使用某种关联数组:

// Insert some data into the cache:
Cache.Insert("TestCache", someValue);
// Retrieve the data like normal:
someValue = Cache.Get("TestCache");

// But, can be done associatively ...
someValue = Cache["TestCache"];

// Also, null checks can be performed to see if cache exists yet:
if(Cache["TestCache"] == null) {
    Cache.Insert(PerformComplicatedFunctionThatNeedsCaching());
}
someValue = Cache["TestCache"];

如您所见,对缓存对象执行 null 检查非常有用。

但我想实现一个缓存清除功能,可以清除缓存值 我不知道整个键名。因为似乎有一个关联 数组在这里,应该是可能的(?)

任何人都可以帮我找出一种循环存储的缓存键和 对它们执行简单的逻辑?这就是我所追求的:

static void DeleteMatchingCacheKey(string keyName) {
    // This foreach implementation doesn't work by the way ...
    foreach(Cache as c) {
        if(c.Key.Contains(keyName)) {
            Cache.Remove(c);
        }
    }
}

Caching in ASP.NET looks like it uses some kind of associative array:

// Insert some data into the cache:
Cache.Insert("TestCache", someValue);
// Retrieve the data like normal:
someValue = Cache.Get("TestCache");

// But, can be done associatively ...
someValue = Cache["TestCache"];

// Also, null checks can be performed to see if cache exists yet:
if(Cache["TestCache"] == null) {
    Cache.Insert(PerformComplicatedFunctionThatNeedsCaching());
}
someValue = Cache["TestCache"];

As you can see, performing a null check on the cache object is very useful.

But I would like to implement a cache clear function that can clear cache values
where I don't know the whole key name. As there seems to be an associative
array here, it should be possible (?)

Can anyone help me work out a way of looping through the stored cache keys and
performing simple logic on them? Here's what I'm after:

static void DeleteMatchingCacheKey(string keyName) {
    // This foreach implementation doesn't work by the way ...
    foreach(Cache as c) {
        if(c.Key.Contains(keyName)) {
            Cache.Remove(c);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身仙ぐ女味 2024-10-12 03:39:30

从任何集合类型中删除项目时不要使用 foreach 循环 - foreach 循环依赖于使用枚举器,该枚举器不允许您从集合中删除项目(如果迭代的集合有项目,枚举器将抛出异常添加或删除)。

使用简单的 while 来循环缓存键:

int i = 0;
while (i < Cache.Keys.Length){
   if (Cache.Keys(i).Contains(keyName){
      Cache.Remove(Cache.Keys(i))
   } 
   else{
      i ++;
   }
}

Don't use a foreach loop when removing items from any collection type- the foreach loop relies on using an enumerator which will NOT allow you to remove items from the collection (the enumerator will throw an exception if the collection it is iterating over has items added or removed from it).

Use a simple while to loop over the cache keys instead:

int i = 0;
while (i < Cache.Keys.Length){
   if (Cache.Keys(i).Contains(keyName){
      Cache.Remove(Cache.Keys(i))
   } 
   else{
      i ++;
   }
}
夜光 2024-10-12 03:39:30

在 .net core 中执行此操作的另一种方法:

var keys = _cache.Get<List<string>>(keyName);
foreach (var key in keys)
{
   _cache.Remove(key);
}

An other way to do it in .net core:

var keys = _cache.Get<List<string>>(keyName);
foreach (var key in keys)
{
   _cache.Remove(key);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文