In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to understand ASP.NET data cache". In daily operation, I believe many people have doubts about how to understand ASP.NET data cache. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand ASP.NET data cache". Next, please follow the editor to study!
Before I talk about ASP.NET data caching, I also want to talk about how to use parameter caching in the page. As mentioned earlier, a cache sets VaryByParam= "none" to no parameters, and we can also set VaryByParam to correspond to the query string value sent with the GET method property, or to the parameter sent using the POST method. When this property 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, an asterisk (*), and any valid query string or POST parameter name. To put it simply, set it to the QueryString name we use on the page. Take a look at an example:
<%... @ Page Language= "C#" AutoEventWireup= "true" CodeFile= "date.aspx.cs" Inherits= "date"% > <%... @ OutputCache Duration= "60" VaryByParam= "CustomerID"% > < 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" / > < 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 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 ());}
After running, clicking 16 and 19 respectively will produce different data according to these two keywords SELECT. At this time, two cache pages will be established according to the two parameters we passed. Please remember the time displayed after each keyword click, and then refresh repeatedly to see how the time has changed! All right, let's talk about data caching.
ASP.NET data caching (Data Caching)
There is a class "Cache" in the System.Web.Caching space through which we can cache data.
The simplest caching method: Cache ["MyCacheString"] = "My CSDN blogging cache!"; create a cache in the form of assignment, and then remove the cache in the form of assignment: myLabel.Text = Cache ["MyCacheString"]. ToString (). This method is very simple to use but has some functional limitations. In order to improve the customized cache, you should use the Cache.Insert () method, as an example:
You only need to put GridView on 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 protected void Page_Load (object sender, EventArgs e) in the database. {dv = (DataView) Cache ["ds"] / / read the data table if (dv = = null) from the ASP.NET data cache / / if the cache is empty, establish a database connection and 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 followed by 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 dataset!") / / this sentence is optional}} protected void Databind () / / Custom data binding method. {GridView1.DataSource = dv; GridView1.DataBind ();}}
Description of ASP.NET data cache parameters
Cache.Insert (String, Object, CacheDependency, DateTime, TimeSpan) 1 is the name of the cache, 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 set to NoAbsoluteExpiration,5 is the adjustable expiration time, if parameter 4 uses a fixed expiration time, then this parameter should be set to NoSlidingExpiration. Oh, are you a little dizzy? let me give you two specific examples to talk about the issue of expiration time.
Cache.Insert ("ds", dv, null,DateTime.Now.AddMinutes (5), System.Web.Caching.Cache.NoSlidingExpiration)
In this example, the cache expires 5 minutes after it is established.
Cache.Insert ("ds", dv, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes (5))
In this example, after the cache is established, the expiration time is adjustable, for example, the cache expiration time established at 1:20 seconds should be 6:20, but if someone accesses the cache at 3:20, the expiration time will be adjusted to 8:20, and so on.
Let's set up a test in VS2005 to see the performance changes before and after using caching. See, it took 0.43 seconds before the cache and only 0.08 seconds after using the cache, the performance difference is more than 5 times!
At this point, the study on "how to understand ASP.NET data cache" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.