Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of Object in httpcontext.current.cache

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.

like image 933
Jeff Avatar asked Jul 06 '09 18:07

Jeff


3 Answers

        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);
        }
like image 109
John Gibb Avatar answered Oct 15 '22 15:10

John Gibb


You can enumerate through the objects:

 System.Web.HttpContext.Current.Cache.GetEnumerator()
like image 6
Tom Ritter Avatar answered Oct 15 '22 15:10

Tom Ritter


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
like image 5
John Saunders Avatar answered Oct 15 '22 14:10

John Saunders