?
Solved

Delete cache in ASP.NET

Posted on 2007-11-27
4
Medium Priority
?
3,477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
MogalManic earned 2000 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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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 …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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