.NET 4.0 Implementing OutputCacheProvider

Posted by azamsharp on Stack Overflow See other posts from Stack Overflow or by azamsharp
Published on 2010-03-12T15:41:43Z Indexed on 2010/03/26 1:13 UTC
Read the original article Hit count: 319

Filed under:

I am checking out the OutputCacheProvider in ASP.NET 4.0 and using it to store my output cache into the MongoDb database. I am not able to understand the purpose of Add method which is one of the override methods for OutputCacheProvider. The Add method is invoked when you have VaryByParam set to something. So, if I have VaryByParam = "id" then the Add method will be invoked.

But after the Add the Set is also invoked and I can insert into the MongoDb database inside the Set method.

 public override void Set(string key, object entry, DateTime utcExpiry)
        {
            // if there is something in the query and use the path and query to generate the key 
            var url = HttpContext.Current.Request.Url;

            if (!String.IsNullOrEmpty(url.Query))
            {
                key = url.PathAndQuery;
            }

            Debug.WriteLine("Set(" + key + "," + entry + "," + utcExpiry + ")");  
            _service.Set(new CacheItem() { Key = MD5(key), Item = entry, Expires = utcExpiry }); 
        }

Inside the Set method I use the PathAndQuery to get the params of the QueryString and then do a MD5 on the key and save it into the MongoDb database.

It seems like the Add method will be useful if I am doing something like VaryByParam = "custom" or something.

Can anyone shed some light on the Add method of OutputCacheProvider?

© Stack Overflow or respective owner

Related posts about ASP.NET