Thursday, April 16, 2015

create a custom OutputCacheProvider

 /// <summary>
    /// A Custom OutputCacheProvider Class.
    /// </summary>
    public class CustomOutputCacheProvider : OutputCacheProvider
    {
        public override object Add(string key, object entry, DateTime utcExpiry)
        {
            if (HttpContext.Current.Request.QueryString["id"] == "2")
            {
                var result = HttpContext.Current.Cache.Get(key);

                if (result != null)
                {
                    return result;
                }

                HttpContext.Current.Cache.Add(key, entry, null, utcExpiry,
                    System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
            }
            return entry;
        }

        public override object Get(string key)
        {
            return HttpContext.Current.Cache.Get(key);
        }

        public override void Remove(string key)
        {
            HttpContext.Current.Cache.Remove(key);
        }

        public override void Set(string key, object entry, DateTime utcExpiry)
        {
            if (HttpContext.Current.Request.QueryString["id"] == "2")
            {
                HttpContext.Current.Cache.Add(key, entry, null, utcExpiry,
                    System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
            }
        }
    }





    <caching>
      <outputCache defaultProvider="CustomProvider">
        <providers>
          <clear/>
          <add name="CustomProvider" type="MvcApplication1.Infrastructure.CustomOutputCacheProvider, MvcApplication1"/>
        </providers>
      </outputCache>
    </caching>


// Register CustomOutputCacheProvider for Child Action Method Caching
            //OutputCacheAttribute.ChildActionCache = new CustomOutputCacheProvider();
   

No comments:

Post a Comment