Solved

Delete cache in ASP.NET

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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