Wednesday, July 25, 2012

Cache Callback

Cache Callback:
Cache Callback provides an ability where by some actions can be performed when that item is removed from cache.

Example: 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace sampleTest
{
    public partial class WebForm3 : System.Web.UI.Page
    {

        static string r = "click";
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Web.Caching.CacheItemRemovedCallback callback = new System.Web.Caching.CacheItemRemovedCallback(OnRemove);
           
            Cache.Insert("key", "1234", null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.Zero,
            System.Web.Caching.CacheItemPriority.Default, callback);

        }

        public static void OnRemove(string key, object cacheItem, System.Web.Caching.CacheItemRemovedReason reason)
        {
            r = "welcome";

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Cache.Remove("key");
            Button1.Text = r.ToString();

        }
    }
}

No comments:

Post a Comment