ASP. NET cache)

Source: Internet
Author: User
What is cache?


Web ApplicationsProgramIt is usually accessed by multiple users. A web site may have a "heavyweight" load, which can slow down the entire server when the site is accessed. When a site is accessed by a large number of users at the same time, slow access speeds are common problems for most websites. To solve this problem, we can use a more advanced hardware configuration, Server Load balancer, and high bandwidth. However, loading is not the only "culprit" for slow-down websites ", therefore, we need to provide a solution that can accelerate data access and improve performance. Using Cache is a good solution.



Cache is a technology that can store the data we usually need. It can temporarily store web pages on a local hard disk for subsequent retrieval. This technology effectively improves the access speed when multiple users access a site at the same time or when one user visits a site multiple times. The cache for Web applications can occur on the client (browser cache) and act on a server between the client and the server (Proxy Cache/reverse proxy cache ), or it only works on the Web server itself (page cache or data cache ).



We can choose to spend a lot of time storing cached data to improve application performance, but this does not really achieve our goal. If we consider the load of the web server, we have to consider the location of cached data storage. Next we will discuss the different cache locations.



 



Different cache locations



The cache of a Web application is either in the client (client browser), between the client and the server (proxy or reverse proxy cache), or on the server (data cache, page output cache ). So we can differentiate the cache location:



1. Client Cache



2. Proxy Cache



3. Reverse Proxy Cache



4. Web Server Cache



1. Client Cache



When the client cache is used, the client browser stores the cached data on the local hard disk as a zero-time file or stores the data in the browser's internal memory for caching. It provides a way to quickly access the same data, because it rejects any network load and server load. This data cannot be shared by other clients, so it is unique to clients.






 



Advantages



1. Because the data is stored on the local client, it is easy to access



2. network transmission is avoided.



Disadvantage



1. the cached data is independent of the browser, so it cannot be shared.



2. Proxy Cache



The main disadvantage of client cache is that the data is stored in the client browser and is private to the client. Proxy Cache uses a unique server to cache data between the server and the client in a shared "location". Therefore, all clients can use the same shared data. Proxy servers (such as Microsoft's proxy servers) can satisfy all web page requests without transmitting requests over the network to the final Web server, which makes quick access a reality.






 



The proxy cache is usually located near the gateway of the network to reduce bandwidth usage. Sometimes, the use of multiple proxy cache servers can relieve the pressure on a large number of users to access the proxy. This is called a cache cluster.






 



Advantages



1. Data is cached on the proxy server and can be easily accessed.



2. Reduce Network Communication



Disadvantage



1. overhead of deployment and the infrastructure to maintain the Proxy Cache Server



3. Reverse Proxy Cache



 Some Proxy Cache servers can be prevented from accessing the front-end of the Web server to reduce the number of requests they receive. It allows the proxy server to process normally accepted requests, and only transmits other "special" requests to the server. This is called reverse proxy.






 



Advantages



1. Data cached on the reverse proxy server can be easily obtained



2. Reduce the number of requests



Disadvantage



1. When the server is configured on the front end of the Web server, it may lead to additional network communication.



4. Web Server Cache



Cached data is stored on the Web server. Data Cache and page cache can use the Web server cache scheme.






 



Advantages



1. Improve the Performance of the site and reduce the overhead of retrieving data from the database and other servers.



Disadvantage



1. Added Network loading overhead.



Cache advantages



1. Reduce server load



2. reduced bandwidth consumption



Cache in ASP. NET



Asp.net provides cache for pages, some pages (page fragments), and data. Caching a dynamically generated page is called the page output cache. When a dynamically generated page is cached, it is only accessed for the first time. Any subsequent requests to the same page will be returned from the cache. Asp.net also provides the cache of part of the page, which is called the cache of part of the page or part of the page. When data is cached, the cached server data (such as data from databases and XML data) can be easily accessed without re-retrieval. Caching reduces the overhead of retrieving data from databases or other data sources. Asp.net provides a "full set of" data cache engines, including clearing (Cache-based priority), expiration, files, keys, and time dependencies. In Asp.net, there are two caching methods to improve performance.






 



In the above image, (1) is used to return the page cache, which means it is used to output the cache, and (2) uses the data cache, you can store data to reduce the overhead of data acquisition.



Asp.net supports two types of expiration policies, which determine when the object will expire or be removed from the cache.



Absolutely expired: Absolute expiration occurs at the time of an identifier. The absolute expiration time is identified as a full Date Format (HH: mm: SS ). At the time of the identifier, the object will expire from the cache.



Asp.net supports three types of cache:



1. Page output cache [Output cache]



2. Page segment cache [Output cache]



3. Data Cache



 



Different types of Cache



1. Page output Cache: Before starting the page output cache, we need to know the process of generating a page, because based on the generated page, we should be able to understand why we should use the cache. An ASPX page is processed in two phases. First,CodeCompiled into msil. Then, during execution, msil is compiled into local code (via JIT, that is, 'Instant compilers '). When we compile the site, the entire code of an Asp.net page is compiled like msil. However, during execution, only some msil is converted to local code, which improves performance.






 



Now, no matter what we get, if a page does not change frequently, JIT does not need to compile it every time. We can use the output cache for pages with relatively static content. Instead of generating a page for each user's request, we can use the page output cache so that it can access itself from the cache. The page only needs to be generated once, And all requests are obtained from the cache. The page output cache allows the entire content of a page to be stored in the cache.






In this figure, when the page is generated for the first request, the page is cached, and the page will be retrieved from the cache for subsequent requests on the same page instead of being generated again.



For the output cache, an outputcache attribute can be directly added to any Asp.net page, specifying the duration of the page being cached (in seconds)



Example


View plainprint?
  1. <% @ PageLanguage="C #"%>
  2. <% @ OutputcacheDuration='123' Varybyparam='None'%>
  3. <Html>
  4. <Script Runat="Server">
  5. Protected void page_load (Object sender, eventargs e ){
  6. Lbl_msg.text=Datetime. Now. tostring ();
  7. }
  8. </Script>
  9. <Body>
  10. <H3>Output cache example</H3>
  11. <P>Page generated on:
  12. <ASP: Label ID="Lbl_msg" Runat="Server"/></P>
  13. </Body>
  14. </Html>


<% @ Page Language = "C #" %> <br/> <% @ outputcache duration = '000000' varybyparam = 'none' %> <br/> <HTML> </P> <p> <SCRIPT runat = "server"> <br/> protected void page_load (Object sender, eventargs e) {<br/> lbl_msg.text = datetime. now. tostring (); <br/>}< br/> </SCRIPT> </P> <p> <body> <br/>


You can also set cache attributes in the background code.


View plainprint?
    1. void page_load (Object sender, eventargs E) {
    2. response. cache. setexpires (datetime. Now. addseconds ();
    3. response. cache. setcacheability (
    4. httpcacheability. Public);
    5. response. cache. setslidingexpiration ( true );
    6. _ MSG. Text = datetime. Now. tostring ();
    7. }


Void page_load (Object sender, eventargs e) {<br/> response. cache. setexpires (datetime. now. addseconds (); <br/> response. cache. setcacheability (<br/> httpcacheability. public); <br/> response. cache. setslidingexpiration (true); <br/> _ MSG. TEXT = datetime. now. tostring (); <br/>}



We have to mention the duration and varybyparam attributes. Duration defines how long the cache will last. Varybyparam defines different cache parameter values






 



As shown in the figure above, if we use a query string for a page, we need to cache all pages based on this parameter. We can use the varybyparam attribute. Based on the query string, data should be cached. When a user requests a page and carries a query string (ID in the image), the page can also be retrieved in the cache. The following example describes how to use the varybyparam attribute.



Example:


View plainprint?
    1. %@ outputcache duration = " 60 " varybyparam = "*" %
Related Article

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.