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.
有没有办法通过缓存查看缓存中的所有对象?我正在动态创建对象,我需要定期浏览列表以清除我不再使用的对象。
7 个解决方案
#1
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
#2
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);
}
#3
You can enumerate through the objects:
您可以枚举对象:
System.Web.HttpContext.Current.Cache.GetEnumerator()
#4
Here is a VB function to iterate through the Cache and return a DataTable representation.
这是一个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
#5
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以及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);
}
#6
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.
由于您可能希望从Cache对象中删除项目,因此迭代它(作为IEnumerable)并不是非常方便,因为这不允许在迭代过程中删除。但是,由于您无法通过索引访问项目,因此它是唯一的解决方案。
A bit of LINQ can however simplify the problem. Try something like the following:
然而,一点LINQ可以简化问题。尝试以下内容:
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
.
请注意,迭代中的每个项都是DictionaryEntry类型。
#7
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.
我不确定我是否理解对象列表部分。
#1
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
#2
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);
}
#3
You can enumerate through the objects:
您可以枚举对象:
System.Web.HttpContext.Current.Cache.GetEnumerator()
#4
Here is a VB function to iterate through the Cache and return a DataTable representation.
这是一个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
#5
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以及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);
}
#6
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.
由于您可能希望从Cache对象中删除项目,因此迭代它(作为IEnumerable)并不是非常方便,因为这不允许在迭代过程中删除。但是,由于您无法通过索引访问项目,因此它是唯一的解决方案。
A bit of LINQ can however simplify the problem. Try something like the following:
然而,一点LINQ可以简化问题。尝试以下内容:
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
.
请注意,迭代中的每个项都是DictionaryEntry类型。
#7
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.
我不确定我是否理解对象列表部分。