I need to remove the data stored in the cache after few minutes (Output and Data Cache).
My concept is binding data from server, storing it in to cache and displayed in the gridview, after a 10 seconds when the button is clicked the cache data should removed and need to show null like nothing. I have build some basic of code, Please any help
to get perfectly. I am new to dotnet.
Please find the below required points and attachments of asp.dot net code and sql query.
1. When page load, gridview should display using Cache.
2. when Clear Cache Button after 10 second from pageload is clicked then the gridview should go hide
3. Reload Cache Button is clicked the gridview again load.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
loadgrid();
}
public void loadgrid()
{
SqlConnection con;
DataView gv1;
gv1 = (DataView)Cache["mysource"];
if (gv1 == null)
{
string str = "Data Source=MyPC; Initial Catalog=Database; Integrated Security=true";
con = new SqlConnection(str);
con.Open();
string select = "select * from cache";
SqlCommand cmd = new SqlCommand(select, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gv1 = new DataView(ds.Tables[0]);
Cache["mysource"] = gv1;
Label1.Text = "Data is Derived from DataBase and Stored in Cache";
}
else
{
Label1.Text = "Data is Derived from Cache";
}
GridView1.DataSource = Cache["mysource"];
GridView1.DataBind();
}
}
SqlQuery
CREATE TABLE cache (id int, name varchar(15), role varchar(15))
insert into cache(1, 'Name1', 'role1')
insert into cache(2, 'Name2', 'role2')
insert into cache(3, 'Name3', 'role3')
insert into cache(4, 'Name4', 'role4')
You can use test for Page.IsPostBack to determine if the page has posted back.
Keep in mind though, the cache is not specific to a single user, it is for every user in the application. If two users are viewing the application they will be fighting over adding items to the cache and you can run into issues when two users try to update
at the same time, who wins.
To control how long an item stays in the cache, look at the following entry for the Cache.Insert() method:
I am not sure about your requirement....why would you first create cache and then remove it....usage of cache is to optimize the performance...so shedding it off for every 10 secs and reloading it is not a good way....
but if you still want to do that way,, create the cache object in If(!IsPostBack) in page_load, then delete it in the button click event....then again you can create it in the other button click event...
n2adn_India
Member
15 Points
75 Posts
Cache Expiration
Feb 22, 2013 02:21 PM|LINK
Hi,
I need to remove the data stored in the cache after few minutes (Output and Data Cache).
My concept is binding data from server, storing it in to cache and displayed in the gridview, after a 10 seconds when the button is clicked the cache data should removed and need to show null like nothing. I have build some basic of code, Please any help to get perfectly. I am new to dotnet.
Please find the below required points and attachments of asp.dot net code and sql query.
1. When page load, gridview should display using Cache.
2. when Clear Cache Button after 10 second from pageload is clicked then the gridview should go hide
3. Reload Cache Button is clicked the gridview again load.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <br /> <asp:Label ID="Label1" runat="server" Text="Status of Cache"></asp:Label> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="Clear Cache" /> <asp:Button ID="Button2" runat="server" Text="Reload Cache" /> </div> </form> </body> </html>Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { loadgrid(); } public void loadgrid() { SqlConnection con; DataView gv1; gv1 = (DataView)Cache["mysource"]; if (gv1 == null) { string str = "Data Source=MyPC; Initial Catalog=Database; Integrated Security=true"; con = new SqlConnection(str); con.Open(); string select = "select * from cache"; SqlCommand cmd = new SqlCommand(select, con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); gv1 = new DataView(ds.Tables[0]); Cache["mysource"] = gv1; Label1.Text = "Data is Derived from DataBase and Stored in Cache"; } else { Label1.Text = "Data is Derived from Cache"; } GridView1.DataSource = Cache["mysource"]; GridView1.DataBind(); } }SqlQuery
AidyF
Star
9204 Points
1570 Posts
Re: Cache Expiration
Feb 22, 2013 02:45 PM|LINK
To control how long things are cached for you need to specifiy the expiration data when adding it to the cache
http://msdn.microsoft.com/en-us/library/system.web.caching.cache.add.aspx
n2adn_India
Member
15 Points
75 Posts
Re: Cache Expiration
Feb 23, 2013 01:52 AM|LINK
But cache regenerated when postback is on.
markfitzme
Star
14433 Points
2227 Posts
Re: Cache Expiration
Feb 23, 2013 02:14 AM|LINK
You can use test for Page.IsPostBack to determine if the page has posted back.
Keep in mind though, the cache is not specific to a single user, it is for every user in the application. If two users are viewing the application they will be fighting over adding items to the cache and you can run into issues when two users try to update at the same time, who wins.
To control how long an item stays in the cache, look at the following entry for the Cache.Insert() method:
http://msdn.microsoft.com/en-us/library/4y13wyk9.aspx
You can use DateTime.Now.AddSeconds(10) to create an expiration 10 seconds from the point of insertion.
ramiramilu
All-Star
95473 Points
14106 Posts
Re: Cache Expiration
Feb 23, 2013 04:56 AM|LINK
I am not sure about your requirement....why would you first create cache and then remove it....usage of cache is to optimize the performance...so shedding it off for every 10 secs and reloading it is not a good way....
but if you still want to do that way,, create the cache object in If(!IsPostBack) in page_load, then delete it in the button click event....then again you can create it in the other button click event...
thanks,
JumpStart
n2adn_India
Member
15 Points
75 Posts
Re: Cache Expiration
Feb 23, 2013 05:53 AM|LINK
Learning Purpose friend, I am new to .net. So testing in different, sorry it may be silly but need to know thats why