Many people know what cache is, and even learn it, but it is really difficult to use it. After several days of learning cache, I want to share with you and discuss it.
I learned with questions. What are the advantages of caching.
Cache stores the acquired data in the memory temporarily. When the acquired data does not change, it still uses the memory. Some people in the comments say that the available files and data inventory are cached. I agree with this, but general programming uses the system. Web. caching. cache class in Microsoft, which stores data in the memory. Someone may ask how to determine whether the obtained data has not changed? This will be discussed later. In this way, the data obtained from the cache is much faster, but the data must change from time to time. The blog rankings in the blog garden are basically unchanged. Although the data is not changing, what should I do with cache when everyone browses the page like this? Directly generate HTML for this page, which is faster to open than to cache.
When a page has both a public part (the same as everyone browses) and a personal part (their own information ). For example, there is a shopping cart in Dangdang. When you purchase things, there is a recommended product on the bottom list (This product may be changed once a day). Of course, the recommended product is related to the product you bought, at this time, HTML cannot be generated, because not everyone browses this page, only when the user buys products related to the recommended products are the same. In this case, we need to use the cache to store the data of this Recommendation product. Of course, this cache key value is related to the purchased product, only recommended products related to the purchased product can be cached.
For example, when you buy books, other good books will be recommended to you. At this time, the recommended books for books are the same, and the called cache is the same, if you buy a computer, you may be advised to give it to you on a variety of good computers. At this time, you can call the cache of the recommended computer.
The above may tell you where the cache is used. Of course, I only know this situation, but there may be other situations. That person knows it and can comment on it.
The advantage of caching is, of course, to reduce the burden on the server and speed up browsing. However, if the cache is not used well, the data may be obtained out of date, or the browser reads slowly. (When you cache frequently changed data)
Is there any client-side cache?
Both are available, but the client cache is embedded in the browser. Basically, the browser automatically processes the cache.
.
The client cache can effectively reduce the server load. First, understand the HTTP header: Last-modified and if-modified-since. In short, last-modified and if-modified-since are both HTTP header information used to record the last modification time of the page, but last-modified is the HTTP header sent by the server to the client, if-modified-since is implemented by the header sent by the client to the server.
In the browser, you can set how the browser uses the cache. The default value is automatic. The procedure is as follows:
Tools-Internet Options-General-settings-check that there are four options in a newer version of the web page to control the use of Cache
Of course, the code can control the Client Cache. For example, the following code can be used to cache things in a browser:
Disable client caching in HTML
<Meta HTTP-EQUIV = "Pragma" content = "no-Cache">
<Meta HTTP-EQUIV = "cache-control" content = "no-cache, must-revalidate">
<Meta HTTP-EQUIV = "expires" content = "wed, 26 Feb 1978 08:21:57 GMT">
How to disable cache in C!
Response. Buffer = true;
Response. expiresabsolute = system. datetime. Now. addseconds (-1 );
Response. expires = 0;
Response. cachecontrol = "no-Cache ";
I know too little about client caching, And we rarely write programs into client caching. I hope someone can provide more about client caching.
The server cache includes system. Web. caching. cahe and memcached.
Of course, system. Web. caching. cahe is a class written by Microsoft, while memcached is a third-party plug-in. Currently, system. Web. caching. cache is not distributed. It can only be deployed on one computer ().
Memcached is a distributed high-speed cache.
Server cache can be divided into three types
1. Output cache:
To implement the page output cache, you only need to add an outputcache command to the page.
<% @ Outputcache duration = "60" varybyparam = "*" %>
Like other page commands, this command should appear at the top of the ASPX page, that is, before any output. It supports five attributes (or parameters), two of which are required.
Duration
Required attribute. The time when the page should be cached, in seconds. Must be a positive integer.
Location
Specify the location where the output should be cached. To specify this parameter, it must be any, client, downstream, none, server, or serverandclient.
Varybyparam
Required attribute. The name of the variable in the request. These variable names should generate separate cache entries. "None" indicates no change. "*" Can be used to create a cache entry for each variable group. Variables are separated.
Varybyheader
Changes cache entries based on changes in the specified header.
Varybycustom
Allow you to specify custom changes (for example, "Browser") in global. asax ").
2. Fragment cache:
Example
<% @ Outputcache duration = "60" varybyparam = "*" %> in this example, the user control is cached for 60 seconds, A separate cache entry will be created for each change in the query string and for each page where the control is located. <% @ Outputcache duration = "60" varybyparam = "NONE"
Varybycontrol = "categorydropdownlist" %> This example caches the user control for 60 seconds and creates separate cache entries for each different value of the categorydropdownlist control and for each page where the control is located. <% @ Outputcache duration = "60" varybyparam = "NONE" varybycustom = "Browser"
Shared = "true" %>
Finally, this example caches the user control for 60 seconds and creates a cache entry for each browser name and major version. Then, the cache entries of Each browser will be shared by all pages that reference this user control (as long as all pages reference this control with the same ID ).
3. The most commonly used programming cache: Data Cache
System. Web. caching. cahe is instantiated by httpruntime. cache or httpcontext. Current. cache. Httpruntime. cache, httpcontext. Current. cache is a built-in object that you want to consider as a seesion.
Differences between httpcontext. Current. cache and httpruntime. cache:
Httpcontext. Current. cache: gets the cache object for the current HTTP request. Httpruntime. cache: Get the cache of the current application. Httpcontext. Current. cache calls httpruntime. cache, and httpcontext. Current. cache is used in web programs, while httpruntime. cache is used in any program. Under the system. Web namespace.
In fact, httpcontext. Current. cache is implemented through httpruntime. cache, so it is best to instantiate it through httpruntime. cache:
Example: system. Web. caching. cache = httpruntime. Cache
There are many methods for system. Web. caching. cache, but the add method contains the cache.
The parameter contains the cache time and dependencies.
The cache time means that when the cache reaches the specified time, the cache becomes invalid, and the dependency item means that the cache becomes invalid when the dependency item changes.
Dependencies include the general dependencies cachedependency and the database dependency sqlcachedependency.
Although the cachedependency class has completed very important functions, its composition structure is relatively simple, mainly including two attributes and a method.
-Attribute "haschanged": determines whether the cachedependency object has been changed.
-Attribute "utclastmodified": returns the modification date of the last dependency.
-Method "dispose": release resources occupied by the cachedependency object. Because the cache class inherits the interface "idispose", this method must be implemented.
Cachedependency
For example, cache. insert ("key", myxmlfiledata, datetime. Now. addminutes (1), new
System. Web. caching. cachedependency (server. mappath ("users. xml ")));
The users. xml file is quite a common dependency. When the XML file is changed, the cache becomes invalid.
Sqlcachedependency is generally added to the database. This setting needs to be set in config and the database service must be started.
In cache. insert, you can set the association with the table in the database. If the table changes, the cache becomes invalid.
Go to the Internet to query how to use