ASP. NET cache learning-data cache

Source: Internet
Author: User

I have discussed how to use the page cache, and now I want to learn how to use the data cache. before talking about the data cache, I also want to talk about using the parameter cache 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 and asterisks.
(*) And any valid query string or post parameter name. 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"> 

protected void Page_Load(object sender, EventArgs e)    {        string conn, 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 = new SqlDataAdapter(comm, conn);        DataSet ds = new DataSet();        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.

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.

Using system; using system. web. caching; using system. data; using system. data. sqlclient; using system. configuration; using system. collections; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; public partial class datacache: system. web. UI. page {dataview DV; // declare a data view to store the data table protect in the database. Ed void page_load (Object sender, eventargs e) {DV = (dataview) cache ["ds"]; // read the data table from the cache if (DV = NULL) // If the cache is empty, establish a database connection 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 = new sqldataadapter (Comm, Conn); dataset DS = new dataset (); DA. fill (DS); DV = Ds. tables [0]. defaultview; // The following sentence is the key. The specific parameters are described later. Insert ("ds", DV, null, system. web. caching. cache. noabsoluteexpiration, timespan. fromminutes (3); databind (); label1.text = datetime. now. tostring (); // time used for reference, optional} else {databind (); response. write ("is cache data !!! "); // This sentence is optional} protected void databind () // custom data binding method {gridview1.datasource = DV; gridview1.databind ();}}

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 !!!

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.