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
<% @ 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/> <p> output cache example </p> <br/> <p> page generated on: <br/> <asp: Label id = "lbl_msg" runat = "server"/> </P> <br/> </body> <br/> </ptml>
You can also set cache attributes in the background code.
Void page_load (Object sender, eventargs e) {<br/> response. cache. setexpires (datetime. now. addseconds (360); <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:
<% @ Outputcache duration = "60" varybyparam = "*" %> <br/> <! Page wocould cached for 60 seconds, and wocould create a separate cache <br/> entry for every variation of querystring -->
The following figure shows the most common and important attributes of the output cache:
All outputcahce attributes we set are from the instance of the system. Web. httpcachepolicy class. The complete implementation of the cache policy provided by Asp.net is encapsulated in the httpcachepolicy class.
Output cache location
As I mentioned earlier, we can store cached data in different places-clients, servers, and proxy servers. Now, how can I set the cache data location. If we store the cached data, it will retrieve the page from the cache, thus saving the page generation time. However, another method is to save data in the client browser, which can reduce network communication. Outputcache can be executed in any of the following three methods: Server, client, and proxy (default ).
The following record shows the cache location details. It shows the cache location and the impact on Cache-control and expires headers.
For example, if you set the client value for the location attribute, the page will not be saved in the cache of the server, but the response will contain a cache-control response header (by using the cache-control header, the page can specify whether they should be cached on a proxy) with a private value and an Expires header (HTTP response, indicating the date and time when the page needs to be re-requested from the server) the value is a time period, which is specified by the Duration Attribute.
Example
<% @ Outputcache duration = '000000' location = 'client' varybyparam = 'none' %>
This will save the cache for 120 seconds. The cached data will not be stored on the server, but should be stored in the client browser.
2. Page segment Cache: Asp.net provides a caching scheme for page fragments, called page fragment caching. To Cache a part of a page, you must first encapsulate this "part of the page" into a user control. Add an outputcache command to the source file of the user control and specify the duration and varybyparam attributes. When a user control is loaded into a page at runtime, it will be cached, and all other pages that reference the same user control will be able to retrieve data from the cache.
The following example shows the details of the cache fragment:
Example
<! -Usercontrol. ascx-> </P> <p> <% @ outputcache duration = '60' <br/> varybyparam = 'none' %> <br/> <% @ Control Language = "'C # '" %> </P> <p> <SCRIPT runat = "server"> <br/> protected void page_load (Object SRC, eventargs e) <br/>{< br/> _ date. TEXT = "User Control generated at" + <br/> datetime. now. tostring (); <br/>}< br/> </SCRIPT> <br/> <asp: Label id = '_ date' runat = "'server'"/>
Here I have cached a user control, so whenever we use it on another page, this part of the page will be cached.
3,Data Cache: Data cache can dynamically provide the performance of an application because it reduces all the "overhead" of data retrieval ". In fact, the data cache stores the requested data in the cache, so that the Web server does not need to request the database server for all incoming requests, which can increase the performance of the web site. To implement data caching, we need to find the data that is accessible and very common. Data cache is also a "full set of features" cache engine that enables you to retrieve and store data between multiple HTTP requests and sessions from the same application.
The image above shows how data is accessed from the north-central database and how the data is retrieved from the cache. Data caching not only caches data in SQL Server, but also stores data from other data sources shown in 1.4.
Now let's take a look at how to implement data caching in Web applications. There are three different ways to add data or objects to the cache. However, based on different solutions, we have different data access methods. These methods are cache [], cache. Add (), cache. insert (). The following table clearly shows the similarities and differences between the three methods:
Cache [] is a very easy-to-use attribute, but cache. insert () and cache. Add () Give us more control over cache data.
Now we need to look at the details of cache. insert () and cache. Add. Cache. insert () has four overload methods, but cache. Add () does not overload the method. The following table shows most of the common attributes of these methods.
The first two are forcibly used by the cache. insert () method, while the other two are different.
Cache dependency
With cache dependencies, we can set dependencies for certain data or entities that may change. Therefore, we can update or remove the cache by setting the cache dependency. Asp.net supports three types of Dependencies:
(1) file-based dependency
(2) Key-based dependency
(3) time-based dependency
File-based dependency: File-based dependency, when the files on the disk change, a common cache item can be invalidated.
When a file changes, some cache items can be forcibly invalidated from the cache. We can set multiple file dependencies. In this case, the dependency should be created on a group of files or folders.
Use: File-based dependency is very useful when you need to update the data based on a file. For example, a news site always obtains data from a file, but if some explosive news comes out, they only need to update the file, and then the cache will expire, in addition, during the expiration time, we can use the onremovecallback callback to reload the updated data in the cache.
Key-based Cache dependency: Key-based dependency. When another cache item changes, a common cache item becomes invalid.
Use: This is useful when we have multiple internal correlated objects in the cache. We need to update or expire all these objects.
Time-based Cache dependency: Time-based dependency will invalidate a cache item at a specified time. The cache. insert () method of cache is used to create a time-based Cache dependency. You can set two types of time:
(1) absolute time
(2) "sliding" Time (relative)
Absolute: Set an absolute expiration time for a cache item. It is a full time format, including (HH: mm: SS ).
Slide: Resets the cache entry expiration time for each request. It is useful when a cache item needs to survive requests from multiple clients.
For all these dependencies, Asp.net allows the following operations to occur:
Automatic failure: Cache items that are in use and do not have any dependencies will automatically expire.
Callback supported: the cache object can be configured to call a piece of code "authorized". When a cache item is removed from the cache, the code will be called. This gives you the opportunity to update the cache. We can use onremovecallback ().
Cache Usage considerations
Output cache considerations:
1. Make the output cache on a page accessible under normal circumstances, and ensure that they return consistent content for all these accesses.
2. When an output cache is used for a page, make sure that incorrect behaviors are not introduced and not processed by specific users.
3. carefully determine the expiration time of the cache page, balance the access speed and memory consumption, and ensure that the cache is consistent with the correct access (throughput ).
4. If you are using varybyparam = '*', consider sliding the page to expire.
Data Cache considerations:
1. Data Cache is not a shared updatable container.
2. the cached data is usually accessed and relatively expensive to obtain.
3. If the data depends on files, folders, or other cached entities, use a cachedependency to ensure that it meets the current needs.
Cache type suggestions
Scenario 1: The generated pages are usually the same, but some displayed forms are usually updated (Use Page fragment Cache)
Scenario 2: The generated page is always changing, but some objects do not change frequently (Use Data Cache)
Scenario 3: The generated page changes every few hours. When information is loaded from the database through an automatic processing process (Use the page output cache and set the expiration time to match the database update time).
For more considerations about Cache Usage, see: 10 common cache errors during development.
----For some content, refer to the Code project!