Solved

Delete cache in ASP.NET

Posted on 2007-11-27
4
3,459 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
ID: 20364680
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
ID: 20385627
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
ID: 20394962
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
ID: 21085969
Here is a Norwegian Cache Dependency article with code examples:
http://www.webressurs.no/main/kode/detalj.asp?kodeid=512
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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