Asp.net httpcontext.current.cache中的对象列表

Asp.net httpcontext.current.cache中的对象列表,asp.net,.net,vb.net,caching,httpcontext,Asp.net,.net,Vb.net,Caching,Httpcontext,是否有办法在缓存中查找缓存中的所有对象?我正在动态创建对象,需要定期浏览列表以清除不再使用的对象。是的,您可以基于缓存键进行索引,也可以迭代内容: For Each c In Cache ' Do something with c Next ' Pardon my VB syntax if it's wrong 您可以通过以下对象进行枚举: System.Web.HttpContext.Current.Cache.GetEnumerator() 下面是一个VB函数,用于迭代缓存并

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

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

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

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

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

下面是一个VB函数,用于迭代缓存并返回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

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

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

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

请注意,迭代中的每个
项都是类型。

Jeff,您应该真正查找缓存项的依赖项。这是正确的做法。对缓存数据(项目)进行逻辑分组,并为组设置依赖项。这样,当你需要使整个组过期时,你碰到了这样的共同依赖项,它们都消失了


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

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

        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);
        }
我在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
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);
   }
C#版本

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
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);
   }
List cacheItemsToRemove=new List();
字符串键=null;
//循环遍历所有缓存项
foreach(System.Web.HttpContext.Current.Cache中的DictionaryEntry c)
{
key=(字符串)c.key;
if(键包含(“prg”))
{
cacheItemsToRemove.Add(键);
}
}
//删除选定的缓存项
foreach(cacheItemsToRemove中的变量k)
{
System.Web.HttpContext.Current.Cache.Remove(k);
}

谢谢大家的宝贵反馈。没想到会有这么多好方法。投票给每个人!谢谢你为什么投反对票?如果你不告诉我哪里错了,我就无法改进。我没有投反对票,但我必须看看其他答案,以确定“c”变量是什么以及如何使用它,在你的例子中。不过,我认为您回答了问题的核心,即如何在缓存中列出对象。提供的解决方案对我不起作用:“缓存”不包含“Where”的定义,并且没有可访问的扩展方法“Where”可以找到接受“cache”类型的第一个参数(是否缺少using指令或程序集引用?