M.Efe Ozer’s Weblog

April 1, 2009

Asp.Net CacheDependency and its Callbacks

Filed under: Asp.Net, C# — Tags: , , , — Mehmet Efe Ozer @ 11:14 pm

 

I am working on a little bit complicated work to reach some information from Second Life.  I need to use a library to do my requests, process them with my classes and need to present on an asp.net website. I will write something about it later.

Just before presentation layer, last callback needed for informed all requested data saved on a xml file. So find a way to track this file is one of the way to handle this problem.

So i decided use cache dependency and CacheItemRemovedCallback.

With cache dependency you create a dependency on a file, on a folder or cached item or items. When your item or items is changed, your dependent item removed from cache.

And it provides two callback that you can catch your item before removed ( CacheItemUpdateCallback ) or after removed( CacheItemRemovedCallback ) from cache.

An important point that these callbacks work with first change on tracked item.So you need to finish your own jobs on your file before creating dependency.  But it gives some options like start to track after definite time past. You need to take a look its overloads.

In my scenario after i reached my data i save it as xml file. File needs to be there so i will create if not. I send a key value pair that i can use in callback for example to understand what is changed in my data file and what was my datafilepath. Xml file creation part is depends on your needs, i just add a root node and an attribute. 20 minute set here for expiration.

private void SetDependency(stringdatafilepath)
    {
       
        XDocument dataxml = newXDocument();
        if(!System.IO.File.Exists(datafilepath))
        {
            dataxml.Declaration
                = newXDeclaration("1.0", "utf-8", "");
            dataxml.Add(newXElement("rootnode"
              
, newXAttribute("rootnodeattr"
                  
, DateTime.Now.ToString())));
            dataxml.Save(datafilepath);
        }
        else
      
{
            dataxml= XDocument.Load(datafilepath);
        }
        CacheDependency dep = newCacheDependency(datafilepath);
        Cache.Insert("keyforitem"
          
, new KeyValuePair<string, XDocument>(datafilepath, dataxml)
            , dep
            , DateTime.Now.AddMinutes(20)
            , TimeSpan.Zero
            , CacheItemPriority.High
            , newCacheItemRemovedCallback(ItemRemovedCallback));
    }

Cachedependency item put in cache with key “keyforitem”, set its value as an keyvaluepair, set 20 mins expiration time. I didn’t try other priority options like “noremove” or else here. (When server resources full server removed items from cache with theirs priority orders.) And finally we assign ItemRemovedCallback.

In my sample i don’t need to use CacheItemUpdateCallback.

Here is CacheItemRemovedCallback

private void ItemRemovedCallback(string key
        , object prevalue, CacheItemRemovedReason reason)
    {
        switch (reason)
        {
            case CacheItemRemovedReason.DependencyChanged:
                {
//my jobs here
                  break;
                }
        }
    }

Dependency Item could be removed from cache by different reasons. Cache priority can be an answer if item removed by memory problems. This sample works on low traffic website so i didn’t faced with problems, also only person is me who change file here.

At the end, if reason is dependencychanged means in this conditions process works ok.

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.