Ehcache (09)--Cache Web page

Source: Internet
Author: User

http://haohaoxuexi.iteye.com/blog/2121782

Page Caching

Directory

1 Simplepagecachingfilter

1.1 Calculatekey

1.2 Configurable initialization parameters

1.2.1 CacheName

1.2.2 Blockingtimeoutmillis

2 Simplecachingheaderspagecachingfilter

3 Simplepagefragmentcachingfilter

Ehcache can also cache Web pages in addition to supporting object caches. This is supported by the filter under the Ehcache-web module. Ehcache automatically gzip compression on response and save the compressed content in the cache. If the client is gzip-enabled, the server will return directly to the response after gzip, otherwise the corresponding response will be extracted from the cache and then returned to the client. If the client is gzip-enabled, it must include "Accept-encoding:gzip" in the header of the request.

1 Simplepagecachingfilter

This is a simple implementation of the page cache filter under the Ehcache-web module for HTTP responses (response) that can be compressed, such as HTML, XML, JSON, and so on. It will use the singleton CacheManager created by the static method of CacheManager, so that if an instance of CacheManager is already present, it will be used directly and will not be created. This is why the Ehcache.xml file or the Ehcache-failsafe.xml file under the classpath is typically taken to create CacheManager, but if Ehcache and spring are integrated in our project, and the Ehcache specified in the spring configuration file Configuration file is not the default location, spring initializes the CacheManager with the specified profile, so that the simplepagecachingfilter is not initialized when CacheManager is used. Instead, it is initialized directly with spring.

Simplepagecachingfilter is useful for caching an entire page, use Simplepagefragmentcachingfilter if you only need to cache a fragment, such as using the part jsp:include contains.

1.1 Calculatekey

As we all know, Ehcache is saving elements in the form of key-value pairs, and the cache for pages is no exception. The key used by the page cache is obtained through the Simplepagecachingfilter Calculatekey () method. Its internal logic is to get the request when the URI and subsequent query string as key to return, such as "/USER/INDEX.JSP?NAME=ABC", which makes it a very wide range of applications. It does not depend on the host name and port number, which makes it equally suitable for situations where multiple domains or multiple ports request the same content. If necessary, we can rewrite the Calculatekey method to implement our own logic of computing key.

1.2 Configurable initialization parameters

For Simplepagecachingfilter, there are two configurable initialization parameters, CacheName and Blockingtimeoutmillis.

1.2.1 CacheName

When using simplepagecachingfilter, we need to specify the name of the cache that the current filter uses for caching by initializing the parameter CacheName (init-param), which corresponds to the name of the cache in the Ehcache configuration file. The default is Simplepagecachingfilter.

<filter>     <filter-name>ehcacheFilter</filter-name>  <filter-class> net.sf.ehcache.constructs.web.filter.simplepagecachingfilter</filter-class>     <init-param>        <param-name>cacheName</param-name>        <param-value>webPageCache</param-value>     </init-param>  </filter>  <filter-mapping>     <filter-name>ehcachefilter</ filter-name>     <url-pattern>/index.jsp</url-pattern>     <dispatcher>forward</ dispatcher>     <dispatcher>INCLUDE</dispatcher>     <dispatcher>request</dispatcher >  

1.2.2 Blockingtimeoutmillis

In the case of multi-threaded concurrency, in order to avoid multiple threads to request the same key and do a repetitive useless work, cachingfilter the bottom of the use of Blockingcache, if the provided cache is not Blockingcache, Then Ehcache will use Blockingcache to encapsulate the current cache. Blockingcache allows concurrent reads to already exist in the cache element, but if the corresponding element does not exist at the time of the read operation, the thread other than the first thread that acquires the lock will be blocked until the corresponding element is placed on the wire. By default, if the first thread does not put the corresponding element into the cache, the threads will remain in a wait state, and the server will be overwhelmed by the fact that the connection with too many requests is not returned because of the long time. To do this, we can solve this problem by initializing the parameter Blockingtimeoutmillis when defining Cachingfilter. By setting the Blockingtimeoutmillis parameter, Blockingcache only blocks the specified time, that is, the blocked thread only blocks the given time, and then throws locktimeoutexception if it times out.

2 Simplecachingheaderspagecachingfilter

Simplecachingheaderspagecachingfilter inherits from Simplepagecachingfilter to provide HTTP cache header information. That is to say, using simplecachingheaderspagecachingfilter is almost the same as using Simplepagecachingfilter. The difference is that the former in the construction of the return information will be set "last-modified, Expires, Cache-control, ETag," the four cache header information, if the information already exists before the setting, then they will be ignored, and directly using Simplecachingheaderspagecachingfilter to regenerate.

Because browsers and other HTTP clients have outdated information that is contained in the return header, they do not need to request the corresponding page again until the information has expired. Simplepagecachingfilter does not set the header information, which means that each client needs to request a page to the server each time the simplepagecachingfilter is used. Then why are you using Simplepagecachingfilter? Consider a scenario where we have a page that contains dynamic data, we cache it, and if you use Simplecachingheaderspagecachingfilter, the return header information will include the expiration information, This also means that the client locally cached page expires before it will not request the corresponding page to the server, and if the server has updated the cache of the page, or removed the cache, the client has cached the page locally until its local information can only wait for the server to get the updated page. At this point, if we are using simplepagecachingfilter, because there is no expiration of the cache header information, the client will each time to the server to initiate requests, so that the server page changes when the client can immediately get to the update. In this case, the role of caching is to reduce the load on the server side, rather than reducing the browser's request.

The Simplecachingheaderspagecachingfilter cached page information is the four header information that contains "last-modified, Expires, Cache-control, ETag". This way, when the corresponding page exists in the cache, all requests get the same header information for the page.

3 Simplepagefragmentcachingfilter

The simplepagefragmentcachingfilter applies to caches that use pages that are contained by Jsp:include. In addition to not gzip response, Simplepagefragmentcachingfilter can do all the things simplepagecachingfilter can do, This is not the simplepagefragmentcachingfilter to do too much repetition of the introduction.

(Note: This article is written based on ehcache2.8.1 and ehcache-web2.0.4)

Ehcache (09)--Cache Web page

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.