Introduction to data caching in ASP. NET data cache

Source: Internet
Author: User

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:

 
 
  1. <%... @ Page Language ="C #"AutoEventWireup ="True"CodeFile ="Date. aspx. cs"Inherits ="Date"%>
  2. <%... @ OutputCache Duration ="60"VaryByParam ="CustomerID"%>
  3. <! DOCTYPE html PUBLIC"-// W3C // dtd xhtml 1.0 Transitional // EN" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <Html xmlns =Http://www.w3.org/1999/xhtml">
  6. <Head runat ="Server">
  7. <Title> ASP. NET data cache </title>
  8. </Head>
  9. <Body>
  10. <Form id ="Form1"Runat ="Server">
  11. <Div>
  12. <Asp: GridView ID ="GridView1"Runat ="Server"BackColor ="LightGoldenrodYellow" 
  13. BorderColor ="Tan"BorderWidth ="1px"CellPadding ="2"ForeColor ="Black"GridLines ="None">
  14. <FooterStyle BackColor ="Tan"/>
  15. <SelectedRowStyle BackColor ="DarkSlateBlue"ForeColor ="GhostWhite"/>
  16. <PagerStyle BackColor ="PaleGoldenrod"ForeColor ="DarkSlateBlue"HorizontalAlign ="Center"/>
  17. <HeaderStyle BackColor ="Tan"Font-Bold ="True"/>
  18. <AlternatingRowStyle BackColor ="PaleGoldenrod"/>
  19. </Asp: GridView>
  20. <Br/>
  21. <Br/>
  22. <Asp: HyperLink ID ="HyperLink1"Runat ="Server"NavigateUrl ="~ /Date. aspx? CustomerID = 16"> 16 </asp: HyperLink>
  23. <Asp: HyperLink ID ="HyperLink2"Runat ="Server"NavigateUrl ="~ /Date. aspx? CustomerID = 19"> 19 </asp: HyperLink>
  24. </Div>
  25. </Form>
  26. </Body>
  27. </Html>Protected VoidPage_Load (ObjectSender, EventArgs e)
  28. ...{
  29. StringConn, comm, id;
  30. If(Request. QueryString ["CustomerID"] =Null)
  31. ...{
  32. Id ="16";
  33. }
  34. Else 
  35. ...{
  36. Id = Request. QueryString ["CustomerID"];
  37. }
  38. Conn ="Server = WEB \ SQLEXPRESS; Uid = moon; Pwd = 1qaz2wsx; Database = store";
  39. Comm ="SELECT * FROM orders WHERE CustomerID ="+ Id;
  40.  
  41. SqlDataAdapter da =NewSqlDataAdapter (comm, conn );
  42. DataSet ds =NewDataSet ();
  43. Da. Fill (ds );
  44.  
  45. GridView1.DataSource = ds. Tables [0];
  46. GridView1.DataBind ();
  47. Response. Write (DateTime. Now. ToString ());
  48. }

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.

 
 
  1. UsingSystem;
  2. UsingSystem. Web. Caching;
  3. UsingSystem. Data;
  4. UsingSystem. Data. SqlClient;
  5. UsingSystem. Configuration;
  6. UsingSystem. Collections;
  7. UsingSystem. Web;
  8. UsingSystem. Web. Security;
  9. UsingSystem. Web. UI;
  10. UsingSystem. Web. UI. WebControls;
  11. UsingSystem. Web. UI. WebControls. WebParts;
  12. UsingSystem. Web. UI. HtmlControls;
  13.  
  14. PublicPartialClassDataCache: System. Web. UI. Page
  15. ...{
  16. DataView dv;// Declare a data view to store data tables in the database. 
  17.  
  18. Protected VoidPage_Load (object sender, EventArgs e)
  19. ...{
  20. Dv = (DataView) Cache ["Ds"];// Read data tables from ASP. NET data cache 
  21.  
  22. If(Dv = null)// If the cache is empty, a database connection is established to read data from the database. 
  23. ...{
  24. String conn, comm;
  25. Conn ="Server = WEB \ SQLEXPRESS; Uid = moon; Pwd = 1qaz2wsx; Database = store";
  26. Comm ="SELECT * FROM orders";
  27.  
  28. SqlDataAdapter da =NewSqlDataAdapter (comm, conn );
  29. DataSet ds =NewDataSet ();
  30. Da. Fill (ds );
  31. Dv = ds. Tables [0]. DefaultView;
  32. // The following sentence is the key. The specific parameters are described later. 
  33. Cache. Insert ("Ds", Dv, null, System. Web. Caching. Cache. NoAbsoluteExpiration, TimeSpan. FromMinutes (3 ));
  34. Databind ();
  35. Label1.Text = DateTime. Now. ToString ();// Reference time, optional 
  36. }
  37. Else 
  38. ...{
  39. Databind ();
  40. Response. Write ("Is Cache Data !!! ");// This sentence is optional 
  41. }
  42. }
  43. Protected VoidDatabind ()// Custom data binding method 
  44. ...{
  45. GridView1.DataSource = dv;
  46. GridView1.DataBind ();
  47. }
  48. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.