Pages

Friday, 21 February 2014

Data Caching in .net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = (DataTable)Cache["user1"];
        GridView1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {

            DataTable dtc = (DataTable)HttpContext.Current.Cache.Get("user1");
            if (dtc == null)
            {
                using (SqlConnection con = new SqlConnection("Data Source=CPU055;Initial Catalog=test;Integrated Security=True"))
                {
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter("select * from test1", con);
                    DataSet ds = new DataSet();
                    da.Fill(ds);

                   // Cache["user1"] = ds.Tables[0];

                    HttpContext.Current.Cache.Insert("user1", ds.Tables[0]);

                   //HttpContext.Current.Cache.Insert("user", ds.Tables[0]);
                    
                   //SqlCacheDependency sqlcache = new SqlCacheDependency("test", "Test1");
                    

                   // HttpContext.Current.Cache.Insert("user", ds.Tables[0], sqlcache, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, new CacheItemRemovedCallback(ReportRemovedCallback));

                    // HttpContext.Current.Cache.Insert("user", ds.Tables[0], cachede, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);

                    // HttpContext.Current.Cache.Insert("user", ds.Tables[0], cachede, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration);

                    GridView1.DataSource = ds.Tables[0];
                    con.Close();
                }

            }
            else
            {
                GridView1.DataSource = (DataTable)Cache["user1"];
            }

            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message + "');</script>");
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        Cache.Remove("user1");
        GridView1.DataSource = (DataTable)Cache["user1"];
        GridView1.DataBind();
        Label1.Text = "deleted";
      
    }

   
    //public static void ReportRemovedCallback(String key, object value, CacheItemRemovedReason removedReason)
    //{

    //    using (SqlConnection con = new SqlConnection("Data Source=CPU055;Initial Catalog=test;Integrated Security=True"))
    //    {
    //        con.Open();
    //        SqlDataAdapter da = new SqlDataAdapter("select * from test1", con);
    //        DataSet ds = new DataSet();
    //        da.Fill(ds);

    //        SqlCacheDependency sqlcache = new SqlCacheDependency("test", "Test1");

    //        HttpContext.Current.Cache.Insert("user", ds.Tables[0], sqlcache, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, new CacheItemRemovedCallback(ReportRemovedCallback));
    //    }

    //}
}

No comments:

Post a Comment