I. Overview
The cache idea can be applied to all layers of software hierarchy. It is an internal mechanism that is imperceptible to the outside world.
The database itself has a cache, And the persistence layer can also be cached. (For example, Hibernate also has cache levels 1 and 2)
The business layer can also have a cache (but in general, this is a process domain, and no cache is set ).
The presentation layer/data service layer (the traditional WEB presentation layer) can also set the cache (JSP cache is the layer to implement the cache mechanism on the app server)
In addition, browser also has cache (such as IE), which is also known to all (implementing the cache mechanism on Web Server ). The higher the upper-layer cache, the better the effect, the more profound the lower-layer cache.
Ii. cache implementation (the browser caches the currently accessed JSP dynamic pages)
(1) server method:
<%
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires",-10 );
%>
(2) Client methods:
Meta is used to simulate HTTP Response Header packets in HTML documents. Meta
Tags are used in There are two types of attributes: Name and HTTP-equiv. The name attribute is mainly used to describe a webpage, corresponding to content (webpage content ), this allows search engines to search for and classify robots. (currently, almost all search engines use online robots to automatically search for Meta values to classify webpages ). The most important of these are description and keywords. Therefore, you should add a meta value to each page. The following are commonly used:
Name attribute
1. <meta name = "generator" contect = ""> describes generation tools (such as Microsoft FrontPage 4.0;
2. <meta name = "keywords" contect = ""> describe keywords Of Your webpage to search engines;
3. <meta name = "Description" contect = ""> tell the search engine the main content of your site;
4. <meta name = "author" contect = "Your name"> tell the search engine the creator of your site;
5. <meta name = "Robots" contect = "All | none | index | noindex | follow | nofollow">
The attributes are described as follows:
Set to all: the file will be retrieved, and the link on the page can be queried;
Set to none: files will not be retrieved, and links on the page cannot be queried;
Set to index: the file will be retrieved;
Set to follow: links on the page can be queried;
Set to noindex: the file will not be retrieved, but the link on the page can be queried;
Set to nofollow: files will not be retrieved, and links on the page can be queried.
HTTP-equiv attributes
1. <meta http-equiv = "Content-Type" contect = "text/html"; charset = gb_2312-80 ">
And <meta http-equiv = "content-language" contect = "ZH-CN"> are used to describe the text and language used by the Home Page. For example, if English is a ISO-8859-1 character set, there are big5, UTF-8, shift-JIS, EUC, Koi8-2 and other character sets;
2. <meta http-equiv = "refresh" contect = "N; url = http: // yourlink"> regularly redirect a webpage to HTTP within the specified time; // yourlink;
3. <meta http-equiv = "expires" contect = "mon, 12 May 2001 00:20:00 GMT"> you can set the expiration time of a webpage, once it expires, it must be called again on the server. Note that the GMT time format must be used;
4. <meta http-equiv = "Pragma" contect = "no-Cache"> is used to disable the browser from accessing the page content from the cache of the local machine, once the page is set, it cannot be called out from the cache;
5. <meta http-equiv = "Set-cookie" contect = "mon, 12 May 2001 00:20:00 GMT"> cookie setting: If the webpage expires, the cookie on the disk will be deleted. Note that the GMT time format must be used;
6. <meta http-equiv = "pics-label" contect = ""> webpage rating: There is a content setting in Internet Options of IE, it can prevent browsing of some restricted websites, and the website's restricted level is set through the meta attribute;
7. <meta http-equiv = "Windows-target" contect = "_ top"> force the page to be displayed on an independent page in the current window, it can prevent your webpage from being called as a frame page by others;
8. <meta http-equiv = "Page-Enter" contect = "revealtrans (duration = 10, Transtion = 50)"> and <Meta
HTTP-equiv = "Page-Exit" contect = "revealtrans (duration = 20, Transtion = 6)"> set the special effect on accessing and leaving the page, this function is called "format/Webpage transition" in FrontPage, but the page to be added cannot be a frame page.
Iii. cache applications
(1) Prevent JSP page caching in order to prevent the browser from caching the currently accessed JSP dynamic page, you can set it as follows:
<%
// Set the expiration date to a previous time
Response. setheader ("expires", "SAT, 6 May 1995 12:00:00 GMT ");
// Set the HTTP/1.1 no-Cache Header
Response. setheader ("cache-control", "No-store, no-cache, must-revalidate ");
// Set the IE extension HTTP/1.1 no-Cache headers. You can add
Response. addheader ("cache-control", "post-check = 0, pre-check = 0 ");
// Set the standard HTTP/1.0 no-Cache header.
Response. setheader ("Pragma", "No-Cache ");
%>
Of course, the Code contained on every page is cumbersome. You can use the custom filter method to process related pages.
(2) Clear page cache in JSP and HTML
1. disable Client caching by adding the following content in
<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 1997 08:21:57 GMT">
Or <meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
2. Disable caching on the dynamic web page of the server. Add a script similar to the following:
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
(3) set a limited time Cache
Int minutes = 10;
Date d = new date ();
String moddate = D. togmtstring ();
String expdate = NULL;
Expdate = (new date (D. gettime () + minutes * 60000). togmtstring ();
Response. setheader ("last-modified", moddate );
Response. setheader ("expires", expdate );
Response. setheader ("cache-control", "public"); /// HTTP/1.1
Response. setheader ("Pragma", "Pragma"); /// HTTP/1.0
Supplement: several suggestions on. jsp cache:
1. jsp cache is best implemented on the filter, and the pages to be buffered are concentrated in the same directory. You only need to modify web. XML to complete the buffer settings for each change, which is more convenient.
2. gzip compression can compress the page very small, with an average compression ratio of 1/3. The hashmap buffer of JSP cache compresses the page, which definitely saves more memory consumption and is more efficient than before compression. for more information about gzip, see this open source project: http://sourceforge.net/projects/pjl-comp-filter