Web Page Caching

Scooter provides an efficient web page caching mechanism which allows web servers to deliver 5000+ pages per second from the page cache.

Turn on web page caching

By default, web page caching is turned on as specified in the generated web.xml file.

  <filter>
    <filter-name>WebPageCachingFilter</filter-name>
    <filter-class>com.scooterframework.web.controller.WebPageCachingFilter</filter-class>
    <init-param>
      <param-name>cacheName</param-name>
      <param-value>WebPageCachingFilter</param-value>    <=== maps to the same name in ehcache.xml
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>ScooterFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter-mapping>
    <filter-name>WebPageCachingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

It is very important that mapping of WebPageCachingFilter appears after mapping of ScooterFilter.

You should remove WebPageCachingFilter from web.xml file if you do not want to cache web pages.

Configure routes for caching

By default, no routes, except GET related RESTful routes, are cacheable.

You can cache web page of a route by one the following two ways.

Using cacheable property in routes.properties

To cache a web page of a route, just add cacheable:true in the definition of the route.

For example, the following code shows that contact page is cached.

routes.name.contact=\
    url:/contact; \
    controller:site; \
    action:contact; \
    cacheable:true

Using cacheablePaths in web.xml

You may also list all request paths in web.xml.

For example, the following code shows that contact and about page is cached, but news page is not.

  <filter>
    <filter-name>WebPageCachingFilter</filter-name>
    <filter-class>com.scooterframework.web.controller.WebPageCachingFilter</filter-class>
    <init-param>
      <param-name>cacheName</param-name>
      <param-value>WebPageCachingFilter</param-value>    <=== maps to the same name in ehcache.xml
    </init-param>
    <init-param>
      <param-name>cacheablePaths</param-name>
      <param-value>/about, /contact</param-value>
    </init-param>
    <init-param>
      <param-name>uncacheablePaths</param-name>
      <param-value>/news</param-value>
    </init-param>
  </filter>

WebPageCachingFilter class

Cache keys

Each web page has a unique key. The key is calculated based on HTTP request method, URI and query strings.

Here are some examples:

GET /posts
PUT /posts/1

Cache removal

WebPageCachingFilter removes cached elements based on HTTP method in RESTful routing.

HTTP verbURLcontrolleractionused forCache Rules
GET /pets pets index display a list of all pets Cache
GET /pets/add pets add return an HTML form for creating a new pet Cache
POST /pets pets create create a new pet Remove all cached pets elements
GET /pets/$id pets show display a specific pet Cache
GET /pets/$id/edit pets edit return an HTML form for editing a pet Cache
PUT /pets/$id pets update update a specific pet Remove all cached pets elements
DELETE /pets/$id pets delete delete a specific pet Remove all cached pets elements

In addition, all cached content will be periodically purged out of cache as specified by the timeToLiveSeconds parameter in ehcache.xml.

WebPageCachingFilter caches all static contents related to URI /static.

Init-Params of WebPageCachingFilter

WebPageCachingFilter supports the following init properties:

  • configFile - the name of Ehcache config file used by the filter; default is ehcache.xml under the WEB-INF directory.
  • cacheablePaths - a list of request paths separated by comma that are suitable for caching, such as /aboutus, /contact, etc.
  • uncacheablePaths - a list of request paths separated by comma that are not suitable for caching, such as /news, /timeline, etc.
  • cacheName - the cache name in ehcache.xml used by the filter.
  • blockingTimeoutMillis - the time, in milliseconds, to wait for the filter chain to return with a response on a cache miss. This is useful to fail fast in the event of an infrastructure failure.

Extend WebPageCachingFilter class

You can easily extend the capability of WebPageCachingFilter class as all methods are of protected scope.