What is ASP. NET data cache learning? How to use ASP. NET data cache? Before talking about ASP. NET data caching, you should first talk about using parameter caching on the page. As mentioned above, if VaryByParam = "none" is set as a cache parameter, you can also set VaryByParam. The set parameter corresponds to the query string value sent with the GET method attribute, or corresponds to the parameter sent using the POST method. When this attribute is set to multiple parameters, the output cache contains a different version of the Request Document for each specified parameter combination. Possible values include none, asterisks (*), and any valid query string or POST parameter names. Simply put, it is set to the QueryString name we use on the page. For example:
- <%... @ Page Language ="C #"AutoEventWireup ="True"CodeFile ="Date. aspx. cs"Inherits ="Date"%>
- <%... @ OutputCache Duration ="60"VaryByParam ="CustomerID"%>
- <! 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> ASP. NET data cache </title>
- </Head>
- <Body>
- <Form id ="Form1"Runat ="Server">
- <Div>
- <Asp: GridView ID ="GridView1"Runat ="Server"BackColor ="LightGoldenrodYellow"
- BorderColor ="Tan"BorderWidth ="1px"CellPadding ="2"ForeColor ="Black"GridLines ="None">
- <FooterStyle BackColor ="Tan"/>
- <SelectedRowStyle BackColor ="DarkSlateBlue"ForeColor ="GhostWhite"/>
- <PagerStyle BackColor ="PaleGoldenrod"ForeColor ="DarkSlateBlue"HorizontalAlign ="Center"/>
- <HeaderStyle BackColor ="Tan"Font-Bold ="True"/>
- <AlternatingRowStyle BackColor ="PaleGoldenrod"/>
- </Asp: GridView>
- <Br/>
- <Br/>
- <Asp: HyperLink ID ="HyperLink1"Runat ="Server"NavigateUrl ="~ /Date. aspx? CustomerID = 16"> 16 </asp: HyperLink>
- <Asp: HyperLink ID ="HyperLink2"Runat ="Server"NavigateUrl ="~ /Date. aspx? CustomerID = 19"> 19 </asp: HyperLink>
- </Div>
- </Form>
- </Body>
- </Html>Protected VoidPage_Load (ObjectSender, EventArgs e)
- ...{
- StringConn, comm, id;
- If(Request. QueryString ["CustomerID"] =Null)
- ...{
- Id ="16";
- }
- Else
- ...{
- Id = Request. QueryString ["CustomerID"];
- }
- Conn ="Server = WEB \ SQLEXPRESS; Uid = moon; Pwd = 1qaz2wsx; Database = store";
- Comm ="SELECT * FROM orders WHERE CustomerID ="+ Id;
-
- SqlDataAdapter da =NewSqlDataAdapter (comm, conn );
- DataSet ds =NewDataSet ();
- Da. Fill (ds );
-
- GridView1.DataSource = ds. Tables [0];
- GridView1.DataBind ();
- Response. Write (DateTime. Now. ToString ());
- }
Click "16" and "19" respectively after the operation to SELECT different data based on the two keywords. At this time, two cache pages will be created based on the two parameters we passed, remember the displayed time after each keyword is clicked, and refresh it repeatedly to see if the time has changed! Now let's talk about data caching.
ASP. NET Data cache Data Caching)
In the System. Web. Caching space, there is a class "Cache". We can Cache data through this class.
The simplest Cache method is Cache ["MyCacheString"] = "My csdn blog !!! "; Create a cache by assigning values, and then retrieve the cache by assigning values: myLabel. text = Cache ["MyCacheString"]. toString (); this method is very simple to use, but it has some functional limitations. To better customize the Cache, you should use the Cache. the Insert () method is as follows:
You only need to put the GridView in the page.
- UsingSystem;
- UsingSystem. Web. Caching;
- UsingSystem. Data;
- UsingSystem. Data. SqlClient;
- UsingSystem. Configuration;
- UsingSystem. Collections;
- UsingSystem. Web;
- UsingSystem. Web. Security;
- UsingSystem. Web. UI;
- UsingSystem. Web. UI. WebControls;
- UsingSystem. Web. UI. WebControls. WebParts;
- UsingSystem. Web. UI. HtmlControls;
-
- PublicPartialClassDataCache: System. Web. UI. Page
- ...{
- DataView dv;// Declare a data view to store data tables in the database.
-
- Protected VoidPage_Load (object sender, EventArgs e)
- ...{
- Dv = (DataView) Cache ["Ds"];// Read data tables from ASP. NET data cache
-
- If(Dv = null)// If the cache is empty, a database connection is established to read data from the database.
- ...{
- String conn, comm;
- Conn ="Server = WEB \ SQLEXPRESS; Uid = moon; Pwd = 1qaz2wsx; Database = store";
- Comm ="SELECT * FROM orders";
-
- SqlDataAdapter da =NewSqlDataAdapter (comm, conn );
- DataSet ds =NewDataSet ();
- Da. Fill (ds );
- Dv = ds. Tables [0]. DefaultView;
- // The following sentence is the key. The specific parameters are described later.
- Cache. Insert ("Ds", Dv, null, System. Web. Caching. Cache. NoAbsoluteExpiration, TimeSpan. FromMinutes (3 ));
- Databind ();
- Label1.Text = DateTime. Now. ToString ();// Reference time, optional
- }
- Else
- ...{
- Databind ();
- Response. Write ("Is Cache Data !!! ");// This sentence is optional
- }
- }
- Protected VoidDatabind ()// Custom data binding method
- ...{
- GridView1.DataSource = dv;
- GridView1.DataBind ();
- }
- }
ASP. NET data cache parameter description
Cache. insert (String, Object, CacheDependency, DateTime, TimeSpan) 1 is the cache name, 2 is the cached data Object, 3 is the cache key dependency, usually Null, 4 is the expiration time. If the relative expiration time is used, it is set to NoAbsoluteExpiration, and 5 is the adjustable expiration time. If parameter 4 uses a fixed expiration time, this parameter must be set to NoSlidingExpiration. Oh, isn't it a bit dizzy? Here are two examples to illustrate the expiration time problem.
Cache. Insert ("ds", dv, null, DateTime. Now. AddMinutes (5), System. Web. Caching. Cache. NoSlidingExpiration );
In this example, the cache will expire in 5 minutes after it is created.
Cache. Insert ("ds", dv, null, System. Web. Caching. Cache. NoAbsoluteExpiration, TimeSpan. FromMinutes (5 ));
In this example, after the cache is created, the expiration time is adjustable. For example, the expiration time of the cache established in seconds should be. But if someone accesses the cache at, the expiration time will be adjusted, and so on ......
Let's create a test in VS2005 to check the performance changes before and after cache usage! No. It took 0.43 seconds before the cache was completed, and 0.08 seconds after the cache was used, the performance was more than 5 times different !!!
The content related to ASP. NET data cache is introduced here. I hope you can understand and learn about ASP. NET data cache.