httpcontext.current.cache 中的对象列表

发布于 2024-07-26 10:08:24 字数 56 浏览 5 评论 0原文

有没有办法查看缓存中的所有对象? 我正在动态创建对象,并且需要定期浏览列表以清除不再使用的对象。

Is there a way to look through the cache for all objects in the cache? I'm dynamically creating objects and I need to periodically go through the list to purge out objects I'm no longer using.

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

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

发布评论

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

评论(7

陪我终i 2024-08-02 10:08:25
        var keysToClear = (from System.Collections.DictionaryEntry dict in HttpContext.Cache
                           let key = dict.Key.ToString()
                           where key.StartsWith("Something_")
                           select key).ToList();


        foreach (var key in keysToClear)
        {
            HttpContext.Cache.Remove(key);
        }
        var keysToClear = (from System.Collections.DictionaryEntry dict in HttpContext.Cache
                           let key = dict.Key.ToString()
                           where key.StartsWith("Something_")
                           select key).ToList();


        foreach (var key in keysToClear)
        {
            HttpContext.Cache.Remove(key);
        }
巴黎夜雨 2024-08-02 10:08:25

您可以通过对象进行枚举:

 System.Web.HttpContext.Current.Cache.GetEnumerator()

You can enumerate through the objects:

 System.Web.HttpContext.Current.Cache.GetEnumerator()
人疚 2024-08-02 10:08:25

是的,您可以基于缓存键建立索引,也可以迭代内容:

For Each c In Cache
    ' Do something with c
Next
' Pardon  my VB syntax if it's wrong

Yes, you can either index based on the cache key, or you you can iterate over the contents:

For Each c In Cache
    ' Do something with c
Next
' Pardon  my VB syntax if it's wrong
云醉月微眠 2024-08-02 10:08:25

下面是一个 VB 函数,用于循环访问 Cache 并返回 DataTable 表示形式。

Private Function CreateTableFromHash() As DataTable

    Dim dtSource As DataTable = New DataTable
    dtSource.Columns.Add("Key", System.Type.GetType("System.String"))
    dtSource.Columns.Add("Value", System.Type.GetType("System.String"))
    Dim htCache As Hashtable = CacheManager.GetHash()
    Dim item As DictionaryEntry

    If Not IsNothing(htCache) Then
        For Each item In htCache
            dtSource.Rows.Add(New Object() {item.Key.ToString, item.Value.ToString})
        Next
    End If

    Return dtSource

End Function

Here is a VB function to iterate through the Cache and return a DataTable representation.

Private Function CreateTableFromHash() As DataTable

    Dim dtSource As DataTable = New DataTable
    dtSource.Columns.Add("Key", System.Type.GetType("System.String"))
    dtSource.Columns.Add("Value", System.Type.GetType("System.String"))
    Dim htCache As Hashtable = CacheManager.GetHash()
    Dim item As DictionaryEntry

    If Not IsNothing(htCache) Then
        For Each item In htCache
            dtSource.Rows.Add(New Object() {item.Key.ToString, item.Value.ToString})
        Next
    End If

    Return dtSource

End Function
世态炎凉 2024-08-02 10:08:25

这可能有点晚了,但我使用以下代码轻松迭代所有缓存项,并执行一些自定义逻辑以删除名称中包含特定字符串的缓存项。

我提供了 VB.Net 和 C# 两个版本的代码。

VB.Net版本

Dim cacheItemsToRemove As New List(Of String)()
Dim key As String = Nothing
'loop through all cache items
For Each c As DictionaryEntry In System.Web.HttpContext.Current.Cache
    key = DirectCast(c.Key, String)
    If key.Contains("prg") Then
        cacheItemsToRemove.Add(key)
    End If
Next
'remove the selected cache items
For Each k As var In cacheItemsToRemove
    System.Web.HttpContext.Current.Cache.Remove(k)
Next

C#版本

List<string> cacheItemsToRemove  = new List<string>();
string key = null;
//loop through all cache items
foreach (DictionaryEntry c in System.Web.HttpContext.Current.Cache)
    {
       key = (string)c.Key;
        if (key.Contains("prg"))
        {
            cacheItemsToRemove.Add(key);
        }
    }
//remove the selected cache items
foreach (var k in cacheItemsToRemove)
   {
       System.Web.HttpContext.Current.Cache.Remove(k);
   }

This may be a bit late, but I used the following code to easily iterate over all cache items and perform some custom logic for removal of cache items that contain a certain string in their names.

I have provided both versions of code in VB.Net as well as C#.

VB.Net Version

Dim cacheItemsToRemove As New List(Of String)()
Dim key As String = Nothing
'loop through all cache items
For Each c As DictionaryEntry In System.Web.HttpContext.Current.Cache
    key = DirectCast(c.Key, String)
    If key.Contains("prg") Then
        cacheItemsToRemove.Add(key)
    End If
Next
'remove the selected cache items
For Each k As var In cacheItemsToRemove
    System.Web.HttpContext.Current.Cache.Remove(k)
Next

C# Version

List<string> cacheItemsToRemove  = new List<string>();
string key = null;
//loop through all cache items
foreach (DictionaryEntry c in System.Web.HttpContext.Current.Cache)
    {
       key = (string)c.Key;
        if (key.Contains("prg"))
        {
            cacheItemsToRemove.Add(key);
        }
    }
//remove the selected cache items
foreach (var k in cacheItemsToRemove)
   {
       System.Web.HttpContext.Current.Cache.Remove(k);
   }
转身泪倾城 2024-08-02 10:08:25

由于您可能希望从 Cache 对象,对其进行迭代(作为 IEnumerable)并不是非常方便,因为这不允许在迭代过程中删除。 但是,鉴于您无法通过索引访问项目,这是唯一的解决方案。

然而,一点 LINQ 可以简化问题。 请尝试如下操作:

var cache = HttpContext.Current.Cache;
var itemsToRemove = cache.Where(item => myPredicateHere).ToArray();
foreach (var item in itemsToRemove)
    cache.Remove(itemsToRemove.Key);

请注意,迭代中的每个 item 的类型均为 DictionaryEntry

Since you potentially want to be removing items from the Cache object, it is not terribly convenient to iterate over it (as an IEnumerable), since this doesn't allow removal during the iteration process. However, given that you cannot access items by index, it is the only solution.

A bit of LINQ can however simplify the problem. Try something like the following:

var cache = HttpContext.Current.Cache;
var itemsToRemove = cache.Where(item => myPredicateHere).ToArray();
foreach (var item in itemsToRemove)
    cache.Remove(itemsToRemove.Key);

Note that each item in the iteration is of type DictionaryEntry.

豆芽 2024-08-02 10:08:25

Jeff,您确实应该查找缓存项目的依赖关系。 这是执行此操作的正确方法。 对缓存数据(项目)进行逻辑分组并为组设置依赖关系。 这样,当您需要使整个组过期时,您就会触及这种常见的依赖关系,并且它们都会消失。

我不确定我是否理解对象列表部分。

Jeff, you should really look up dependencies for your cached items. That's the proper way of doing this. Logically group your cached data (items) and setup dependencies for your groups. This way when you need to expire the entire group you touch such common dependency and they're all gone.

I'm not sure I understand the List of Object part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文