Solved

Delete cache in ASP.NET

Posted on 2007-11-27
4
3,469 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
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…

688 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