Solved

Delete cache in ASP.NET

Posted on 2007-11-27
4
3,447 Views
Last Modified: 2012-06-21
How do I delete cache where part of the cachekey is something?

Example:

public class Cache
{
    public static void SetCache(string cacheKey, object value, int cacheTime)
    {
        System.Web.Caching.Cache cache = System.Web.HttpRuntime.Cache;
        cache.Insert(cacheKey, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 3600));
    }

    public static void RemoveCache(string cacheKey)
    {
        System.Web.HttpRuntime.Cache.Remove(cacheKey);
    }

    public static object GetCache(string cacheKey)
    {
        System.Web.Caching.Cache cache = System.Web.HttpRuntime.Cache;
        return cache.Get(cacheKey);
    }
}

//This is how I set the cacheKey:
string cacheKey = string.Format("ArticleData_{0}", (int)Article.ArticleId);

//This is how I get Article from cache
Article CacheArticle = (Article)Cache.GetCache(cacheKey);

//This is how I delete cache:
Cache.RemoveCache(cacheKey)

BUT: HOW DO I DELETE CACHE WHERE PART OF THE CACHEKEY IS SOMETHING?
So: I want to remove all cache where cachekey contains the string "Article". This means all ArticleData cache objects and other cache objects that contain the string "Article".
0
Comment
Question by:webressurs
  • 2
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
Comment Utility
You have two options:

1) When delete occurs, iterate through the list of all of the public items in the cache and remove the ones that match:
      ArrayList removeList=new ArrayList()
      foreach(DictionaryEntry entry in Cache) {
          if (entry.Key.ToString().Contains(key))
               RemoveList(entry.Key);
      }
      foreach(string key in RemoveList)
         Cache.Remove(key);
2) Keep a list of keys in your Cache object and use that list in the remove method:

    HashTable cacheKeyList=new HashTable();
    public static void SetCache(string cacheKey, object value, int cacheTime)
    {
        cacheKeyList[cacheKey]=DateTime.Now;
        System.Web.Caching.Cache cache = System.Web.HttpRuntime.Cache;
        cache.Insert(cacheKey, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 3600));
    }

    public static void RemoveCache(string cacheKey)
    {
        ArrayList keyNames=new ArrayList()
        foreach(string key in CacheKeyList.Keys) {
          if (key.Contains(cacheKey)) {
              keyNames.Add(key);
          }
        }
        foreach(string keyName in keyNames) {
          CacheKeyList.Remove(keyName);
          System.Web.HttpRuntime.Cache.Remove(keyName)
        }
    }

The 2nd solution may store keys that were ALREADY removed from the cache because of expiration and/or memory.  The 2nd solution also consumes more memory.

The 1st solution will remove ANY item that matches.  Even items that were NOT added by this class.

Both solutions may delete too many entries (Cache.Delete("2") will remove "Article_2", "Article_20", and "Article_22").  You could change the code to only remove 1st match and/or use the EndsWith() method instead of Contains().
0
 
LVL 1

Author Comment

by:webressurs
Comment Utility
Heard from a guy that cache dependensies can be used for making this, but dont understand how... any clue?
0
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 500 total points
Comment Utility
A cache dependency is a way to remove an item from the cache based on an external event (table change, file change, ...).  There are built in dependency objects for SQL Server 2005 table changes and File cache dependencies.

Here is an example for creating custom dependencies:
  http://msdn.microsoft.com/msdnmag/issues/04/07/CuttingEdge/ - custom cache dependencies

Is the reason you want to delete from the cache an external event?  Or is it an event inside your application?
0
 
LVL 1

Author Comment

by:webressurs
Comment Utility
Here is a Norwegian Cache Dependency article with code examples:
http://www.webressurs.no/main/kode/detalj.asp?kodeid=512
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now