Transfer from Zhang Xiaoxiang: Research on Cache Problem in Web Development

xiaoxiao2021-04-05  266

Under normal circumstances, the browser caches the already accessed page content. About how to prohibit the introduction of the browser cache, there is related articles everywhere, how to use the cache, how to handle the cache, but fresh Some people talk about. I have been confused for this question. This problem is also a problem that most experienced Web developers. I have done dozens of big and small web projects when communicating with them. When these problems have encountered and resolve these problems in the project, they always think about the reasons and details of these issues without enough time and energy, they always know the semi-solving, vague, and currently There are very few specifications and detailed explanations for these issues. I have recently used the time to study the browser cache, mainly including the details.

1. How to prohibit browser caching, this is the easiest issue, it is ashamed to explain here, but for integrity, it may be listed as a knowledge point.

2. When the browser accesses the cached resource, it will send a request to the server in what circumstances. Under what circumstances do not send a request to the server. This is related to the browser's cache settings! However, due to almost all People's browser are the default settings, so the key points should be placed on the research on the default cache settings of the browser.

3. When accessing a cached resource through the hyperlink in other web documents, whether the browser wants to send a request to the server? If it does not send, there will be a problem: when selling a product, When you see the display page of the goods, you will see the content you see, not the updated inventory data. However, when accessing a normal HTML file, if the browser sends a access request to the server, the efficiency will be relatively low, which will lose the meaning and value of the cache. So, the conclusion should be the browser access dynamic page Caches should not be used, and the cache should be used when accessing the static page, but only according to the resource name of the accessed page, the browser cannot know that the display page of the product inventory is dynamic, or is still static. How is the browser to determine whether it is dynamic or static? Under what request, it always issues a new request for the cache resources?

4. For cached content, even if the browser sends a request to the server, the server may not return content after receiving the request, but let the browser continue to use the cache, what is the benefit in practical applications? How do I handle their specific details?

5. The server side also has a cache. After the server receives the browser's request, it is assumed that it returns the response content, but the return of the response may not be the latest content, and it is likely to be an old cache version. What is going on? ?

All of these issues have profound analysis and detailed experimental steps in the book of the author's "In-depth experience Java Web development insider".

The following is the exception of the book (excluding the interpretation of the server-side cache technology, server-side cache technology has case analysis in other chapters)

4.5.8 Browser Cache Insider Defines a getLastModified method in the httpservlet class in the httpservlet class. The full syntax defined as follows: protected long getlastmodified (httpservletRequest Req) The return value is 0:0:00 on January 1, 1970 One milliseconds calculated in 0 seconds, the getLastModified method defined in the HTTPServlet class always returns a negative number, and this method can be overridden in the HTTPServlet subclass to return a modification time representing the current output, Httpservlet class The Service method can automatically generate a Last-Modified header field in the response message according to this return value. In general, both browsers can cache the already accessed page content, and the return value of the getLastModified method can affect how the browser handles and utilizes cache content. Before you understand the application of the GetLastModified method, you should first understand the browser's cache mechanism. Click the "Tool" "Internet Options" menu of the IE browser to open the Internet Options dialog, then click the "Settings" button in the "Internet Temperature File" column in the General tab. The Settings dialog shown in Figure 4.16. Figure 4.16

In the Internet Temporary folder bar of the Settings dialog box shown in Figure 4.16, you can see the full directory name of the browser saved all the folders of all cache page content. For the Administrator user, its default setting is "C: / documents and settings / administrator / local settings / temporary internet files". You can open this folder as shown in Figure 4.17. .

Figure 4.17

It can also be seen from Figure 4.16 that the settings of its "newer version" function items have 4 options, just click the Question mark button in the "Settings" dialog header bar, then click the appropriate Options, you can see the functions and meaning of each option:

"Check each time the page" option indicates that the browser has access to a page each time, no matter whether the browser has cached this page, you must send an access request to the server. The advantage of this setting is very powerful, and it is certainly able to access the latest content of the web page, but if the content of the webpage is rarely updated, the access efficiency of this setting is relatively low. "Check every time INTERNET Explorer" option indicates that during each start operation of the browser, when the browser is accessed, no matter whether the browser caching this page, you must send a request to the server, but in browsing Subsequent access to this page during this startup operation, the browser will no longer send access requests to the server, but directly using the contents of the cache. This setting has high access efficiency, and it also takes into account better real-time, which guarantees that all the latest web content is available after the browser is started. "Auto" option is similar to "Check each time the Internet Explorer" option is similar, but the access to the image is different. If the browser finds the image update on the page over time, this, even When the browser performs the first access to a cached image, it does not necessarily send a access request to the server, but simply uses the content in the cache. The "Auto" option is the default setting of the browser, so almost everyone's browser is working in this way. The role and meaning of this option should be the focus of readers. The "Do not check" option indicates that the browser will access a page in the case, as long as you can find the cache information of this page locally, the browser does not send an access request to the server, but directly uses the cache. The advantage of this setting is that the access efficiency is high, but if the web page content of the server is updated, the content seen by the browser is likely to expire.

In the browser's "New Release of the Saved Version" of the browser adopts the default "automatic" setting item, if the browser has just accessed a web page, the server updated the content of this page, when browsing When it is closed and re-accessed this page, the user sees will not be updated web content, but the expired web content. In order to improve the browse efficiency, this time the expiration information of such a little probability should be allowed when accessing the static web content, and these expiration information will not cause anything bad, just like you have seen the day before. News, not the news, what is the problem? However, if the browser accesses a dynamic web page, this will ask the browser to see the latest content every visit during its entire run, for example, return a product after returning to the product inventory display page When you see it should be an updated inventory data, and it should not be the previously seen. Only based on the resource name of the visited page, the browser cannot know that the display page of the item inventory is dynamic content, or is still static. For this case, the browser will process whether or not to include the Last-Modified header field in the response message, if the response message does not contain a last-modified header field, it will send a request to the server each time you access this page. Otherwise, it only issues access requests to the server only after each startup run, and the subsequent access to this page will no longer send an access request to the server during startup operation. In Chapter 2, the Last-Modified header field in the response message can be used to specify the last update time of the response content, and when the client comes to this document, it will be based on the last-modified head in the later request message. The time specified by the field generates the IF-Modified-Since request header field to indicate the last update time of the cache document. The server will return document content only if the modification time of the document is new than the time specified by the if-modified-since request header. If the web page content has not been modified since IF-Modified-Since, the server will return a 304 (Not Modified) status code to indicate that the version of the browser cache is the latest, but will return document content to the browser. The browser continues to use the previous cache. In this way, the amount of communication data between the browser and the server can be reduced to a certain extent, thereby improving communication efficiency. The HTTPSERVLET class provides an HTTPSERVLET overloaded Service Method for the HTTPservlet when the servlet program of the HTTPSERVLET class is inherited. Before calling the Doget method, it will also call the getLastModified method, and decide whether to call the Doget method and generate a last-modified header field in the response message in response messages according to the return value of the getLastModified method. The specific rules are as follows:

When the getLastModified method returns a negative number, the Service method will directly call the Doget method directly to generate a response content, regardless of the request message, which is the behavior of the getLastModified method defined in the HTTPSERVLET class; when the getLastModified method returns a positive number, And when the request message is not included in the IF-Modified-Since request header (this often occurs when the first access to a certain resource), or the time value ratio in the IF-Modified-Since request header included in the request message When the time value returned by the getlastModified method, the service method generates a Last-Modified header field according to the return value of the getLastModified method, then call the DOGET method to generate a response content; when the getLastModified method returns a positive number, and the IF included in the request message The time value in the Modified-Since request header is new or in the same time than the time value returned by the getLastModified method, the Service method will not call the Doget method, but return a 304 (Not modified) status code to the browser indicates that the browser can use Its previous cache content. Hand experience: Uncover the mystery of browser cache

(1) Write a servlet program called CacheServlet, print out the current time value in the command line window of the browser and Tomcat in its doget method, and the getLastModified method is also printed in the Tomcat command line window. Values ​​and return to the current time value, first comment the getLastModified method, as shown in routine 4-9.

Routines 4-9 CacheServlet.java

Import java.io. *; import javax.servlet. *; import javax.servlet.http. *;

public class CacheServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PrintWriter out = response.getWriter (); long now = System.currentTimeMillis (); out.println ( "doGet:" now) ; System.out.println ( "doGet:" now);} / * protected long getLastModified (HttpServletRequest req) {long now = System.currentTimeMillis (); System.out.println ( "getLastModified:" now); return Compile the cacheservlet.java file to ensure that the Class file generated after compile is placed in the D: / MyWeb / Web-INF / CLASES directory.

(2) Write a web file named cachetest.html in the D: / MyWeb directory, as shown in routines 4-10.

Routine 4-10 Cachetest.html

Cache Test

(3) To facilitate viewing how the browser generates cache content, it is best to remove all cached content saved in the Internet Temporary folder. Click the "Tool" "Internet Options" menu of the IE browser, open the Internet Options dialog shown in Figure 4.16, then click "Internet Temporary File" field in the General tab. Delete Files button, which deletes all cache content saved in the Internet Temporary folder. Click the "Settings" button in the "INTERNET Temporary File" field in the General tab, click the "View File" button in the Internet Temperature Folder box in the "Settings" dialog box. Figure 4.17 The Internet Temporary folder shown, you can see files that no longer have any cache. In addition, in the Settings dialog box, you also need to recover the function items of "Check Saved Version" to the default Auto. Enter the following address in the browser address bar: http: // localhost: 8080 / IT315 / Cachetest.html In the result page displayed in the browser window, click Cache Test "hyperlink to access the CacheServlet. Open the Internet temporary folder again, then you can see the cache files that have generated the cachetest.html and cacheservlets just visited, as shown in Figure 4.18. The CacheServlet cache file is selected, which can be seen from the summary information displayed in the Windows Explorer window, the last modification time recorded in the CacheServlet cache file is "no". Figure 4.18

(4) Perform a Telnet 127.0.0.1 8080 command in the command line window, after connecting to the Tomcat web server, then in the Telnet program command window, enter the following: GET / IT315 / Servlet / CacheServlet http / 1.1 Host : The result shown in the Telnet window is shown in Figure 4.19.

Figure 4.19

As can be seen from Figure 4.19, there is no last-modified header field in the response message returned by CacheServlet, which is why the last modification time of the CacheServlet cache file displayed in Figure 4.18 is "no".

(5) Back to the browser window, briefly remember the time value displayed in the browser window and Tomcat's command line window, then click "Back" and "Forward" buttons in the browser toolbar, you can Seeing that the contents of the CacheServlet page each showing is not changed, there is no new information in the Tomcat command line window. The same is true for entering the access address of the CacheServlet directly in the browser address bar. This illustrates that when accessing the cached page with the browser's "Back" or "advance" mode, or when accessing the cached page in the browser address bar, the browser will directly call the cache, and do not send it to the server. New access request. In the Windows Explorer window shown in Figure 4.18, remove the CacheServlet page, use the Back and "Back" button to access the CacheServlet, or enter the access address of the CacheServlet directly in the browser address bar, this time the browser New time values ​​will be displayed in the command line window of the window and Tomcat. This shows that when the cache file is no longer existed, use the "Back" and "Forward" button to access the CacheServlet, or enter the access address of the CacheServlet directly in the browser address bar, the browser will send a new access request to the server. . Back to the Cachetest.html page via the "Back" button in the browser toolbar, then click the hyperlink to access the CacheServlet, you can see the browser window and Tomcat's command line window. New time value. Repeat this process, you can see that the CacheServlet page can display a new time value each time. This shows that the browser will send a new access request to the server through the hyperlink in other web documents, the browser will send a new access request to the server instead of calling Cache content. (6) Modify the CacheServlet.java source file, cancel the comment to the getLastModified method, recompile the CacheServlet.java source file, wait for Tomcat to reload the CacheServlet, refresh the access to the CacheServlet in the browser window. Open the Internet temporary folder (if the folder is turned on, press the F5 button to refresh), select the CacheServlet cache file, can be seen from the summary information displayed in the Windows Explorer window, this CacheServlet There is a record message for the last modified time in the cache file, as shown in Figure 4.20. Figure 4.20

(7) Repeat step (4) operation, the result shown in the Telnet window is shown in Figure 4.21.

Figure 4.21

As can be seen from Figure 4.21, there is a last-modified header field in the response message returned this time, which is the cause of the CacheServlet cache file displayed in Figure 4.20 has a recording information for the last modified time. (8) Back to the browser window, briefly remember the time value displayed in the browser window and Tomcat's command line window, and then repeatedly click "Back" and "Forward" button in the browser toolbar. Seeing that the contents of the CacheServlet page each showing is not changed, there is no new information in the Tomcat command line window. The same is true for entering the access address of the CacheServlet directly in the browser address bar. This is exactly the same as the experimental effects seen in step (5). Back to the Cachetest.html page via the browser toolbar, click the hyperlink to access the CacheServlet, then see the content displayed in the browser window and Tomcat command line window. There is still no change, repeating this process multiple times, and the results seen have not changed. This situation is very different from the experimental effects seen in step (5). Through the comparison of the experimental effects of the two, we can draw the following conclusions: if a page response message contains a Last-Modified header field, When this page is called through hyperlink in other web documents, the browser issues an access request to the server only when the page will be accessed each time, and the subsequent pairs of this page during this startup operation Access, the browser will no longer send access requests to the server, but call the cache directly. When accessing a normal HTML file, Tomcat's default servlet generates a last-modified header field to illustrate the latest modification time of this HTML file, so it is only in an HTML during the boot run. When the file is performed for the first time, it will send a real access request to the server. For the page of the HTML file, its content is in the case of change between the first and subsequent access to the page during a certain run of a browser, and even if this happens. What big issues don't have, so this process of Tomcat's default servlet is completely reasonable and should be.

If there is no Last-Modified header field in a page's response message, when this page is called through the hyperlink in other web documents, the browser will send each access to the page throughout the startup operation. Send an access request without calling the cached content. Therefore, as long as there is no Last-Modified header field in the dynamic webpage program, you don't have to worry about accessing the dynamic webpage program through the hyperlink to access the request to the server.

Refreshing access to CacheServlet in the browser window, the refresh is to let the browser must send a request to the server, and new content is displayed in the browser window. View the command line window that launches Tomcat, you can see two new information similar to the following: getLastModified: 1147408274473 doget: 1147408274473 This Description CacheServlet After calling the getLastModified method, the Doget method is then called. Because the getLastModified method returns the current time, it is certainly the time value specified by the IF-Modified-Since request header field in the request message issued by the browser (that is, the "current time" that is returned last time is called last time ("current time") to be new. Therefore, after the CacheServlet calls the getLastModified method, the Doget method is then called, so the new content is seen in the browser. (9) CacheServlet.java modified source file, so that the last piece of the method getLastModified return statement returns a fixed time value, as follows: protected long getLastModified (HttpServletRequest req) {long now = System.currentTimeMillis (); System. Out.println ("getLastModified:" NOW); RETURN / * Now * / 1234;} Re-build the cacheservlet.java source file, wait for Tomcat to reload the CacheServlet, newly start a browser program to access the cachetest.html page, then single Cache Tests "hyperlinks in the cachetest.html page Access CacheServlet, the contents of the CacheServlet page displayed at this time is still the final content in step (8), there is no change! You can see the new information like the Tomcat, you can see the new information similar to the following: getLastModified: 1147414361586 This phenomenon shows that the browser has just issued a request to the server. Since the request message issued by the browser has an IF-Modified-Since request header field, its setting value is the time specified by the last Last-Modified header field, and the GetLastModified method in the cacheservlet is the time value returned in this time than last. -Modified header field is older (in general, it should be equal, here for simplifying the program writing, just returning 1234 This comparison time value, but does not affect the experimental effect), so CacheServlet does not call Doget Method (no prompt information called by the Doget method) in the command line window of Tomcat), but returns a 304 (Not Modified) status code to the browser to indicate the latest version of the browser cache, resulting in the browser still The last cached content is displayed.

转载请注明原文地址:https://www.9cbs.com/read-132020.html

New Post(0)