httpcontext.current.cache中的对象列表

Jef*_*eff 6 .net vb.net asp.net caching httpcontext

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

Joh*_*ibb 18

        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);
        }
Run Code Online (Sandbox Code Playgroud)


Tom*_*ter 6

您可以枚举对象:

 System.Web.HttpContext.Current.Cache.GetEnumerator()
Run Code Online (Sandbox Code Playgroud)


Joh*_*ers 5

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

For Each c In Cache
    ' Do something with c
Next
' Pardon  my VB syntax if it's wrong
Run Code Online (Sandbox Code Playgroud)