Chapter 7. JEUS WebCache

Table of Contents

7.1. Overview
7.2. JSP Caching
7.2.1. Basic Information
7.2.2. <cache> Tag
7.2.3. Example of Using <jeus:cache>
7.2.4. Using Flush
7.2.5. Using Refresh
7.3. HTTP Response Caching
7.3.1. Configuring Filters

This chapter describes how to use JEUS WebCache to improve the web application performance.

7.1. Overview

Performance is an important concern for any application, but it becomes a critical factor when the application is a web service that is accessed by thousands of clients simultaneously. There are many ways to improve web service performance.

A resolution from the hardware side is to improve response times by installing more servers and distributing the requests on multiple servers with load balancing. However, this increases costs and the complexity of managing and operating servers due to factors including clustering.

A resolution from the software side is to cache frequently-used data without server scaling. For subsequent requests, applications can use the cached data instead of regenerating the data, which improves response times and increases performance.

This section describes how to use JEUS WebCache in the JEUS system to improve the performance of web applications. The following are the provided caching methods.

  • JSP Caching

    Partially caches JSP pages by using the tag library. This is useful when there are requests for partially modified JSP pages.

  • HTTP Response Caching

    Caching the entire HTTP response. This is useful when there are requests for static contents.

Entries that are cached in JEUS WebCache use soft references, which can prevent OutOfMemory errors from occurring.

To make the best use of caching, select pages that are frequently used or incur long response times due to complex database queries as caching targets.

7.2. JSP Caching

JSP caching improves the performance of web applications by storing parts of JSP pages in JEUS WebCache by using the JSP tag library.

7.2.1. Basic Information

JEUS WebCache uses <jeus:cache> as a user-defined (custom) tag.

When putting a JSP page content in <jeus:cache>, the body content of the tag is created when the first request occurs. The content will be sent in the response to be cached. The cached content will be returned when the next request occurs.

The <jeus:cache> tag can be used as in the following.

<%@ taglib uri=”http://www.tmaxsoft.com/jeuscache” prefix=”jeus” %>

<jeus:cache name=”...” key=”...” scope=”...” timeout=”...” 
    size=”...” async=”...” df=”...”>
   . . . Body content to be cached. . .
</jeus:cache>

The flush attribute uses a self-closing tag (/>) and is described in "7.2.2. <cache> Tag".

The algorithm used in JSP Caching is LRU. If the number of JEUS WebCache entries exceeds the maximum allowed number, the cached entries will be deleted according to the LRU algorithm.

The TLD file (Tag Library Descriptor) is included in the 'jeus.jar' file that is deployed. To send the URI information from the 'jeuscache.tld' file to the JSP engine, 'taglib uri' in the <jeus:cache> tag must be set to 'http://www.tmaxsoft.com/jeuscache'.

The following diagram shows the data structure that is used to cache entries by using the 'name' attribute and the 'name' + 'key' attributes.

[Figure 7.1] Cache Data Structure

Cache Data Structure


In the previous figure, Name1 and Name3 are cached by only using the 'name' attribute as a tag. If the attributes 'name' and 'key' are used as tags, entries are cached like Name2.

7.2.2. <cache> Tag

The jeuscache.tld file defines the user-defined tag <jeus:cache>. This file is included in jeus-servlet.jar and is located in the following path.

jeus/servlet/cache/resource/jeuscache.tld

The following example shows how to configure the <cache> tag in the jeuscache.tld file.

[Example 7.1] Configuring <cache> Tag: <<jeuscache.tld>>

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag
Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>jeuscache</short-name>
    <uri>http://www.tmaxsoft.com/jeuscache</uri>
    <display-name>JEUSCache Tag Library</display-name>
    <tag>
        <name>cache</name>
        <tag-class>jeus.servlet.cache.web.tag.CacheTag</tag-class>
        <body-content>JSP</body-content>
        <description>JEUS WebCache</description>
        <attribute>
            <name>flush</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>timeout</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>scope</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>name</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>size</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>key</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>async</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>df</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>


The following describe the attributes of each tag.

TagDescription
flush

Used to delete cache entries. To remove an entry, specify the 'name' attribute or the 'name' + 'key' attributes.

When both 'name' and 'key' are used to cache the entries, if the 'flush' attribute only uses the 'name' attribute, all entries that have the 'key' attribute as an identifier will be deleted. Unlike other attributes, the 'flush' attribute uses a self-closing tag (/>) without any content.

timeoutThe cache period in the Simple Date Format. (Default value: 1 hour). Use a string that is a combination of a number and a character representing time. The valid characters that represent the time are 's' (seconds), 'm' (minutes), 'h' (hours), 'd' (days), and 'w' (weeks). For example, 10s indicates 10 seconds, 10d indicates 10 days, and 4w indicates 4 weeks. The default time character is seconds, so if a number is entered without a character, it is considered as seconds. If set to 0, the body content will not be cached. If set to -1, the cached content will not expire until it is forcibly flushed.
scopeThe scope of the cached entries. Options are 'application' or 'session'. (Default value: application)
name

This allows multiple pages to share the cached data. The name must be unique within the scope.

If not set, it will be created based on the URI. If the body content of jeus:cache does not need to be shared, do not use this attribute.

sizeThe maximum number of objects that can be cached. If the number of cached objects exceeds this value, objects in the cache will be deleted using the LRU algorithm. The maximum value should be set according to each web application.
key

Another value that identifies cached entries. This attribute must be used with the 'name' attribute, so the 'name' + 'key' value is used as a unique identifier for an entry.

The 'key' attribute can be set to a list of scopes as in the following.

<jeus:cache name=”. . . ” 
 key=”[parameter|page|session|request|application].keyname” . . . >

In the previous example, the 'keyname' value must be set when sending a request. For 'parameter', 'page', or 'request', the 'keyname' must be set to the key value from the request page that matches the cached page. For 'application' and 'session', it can be set from other pages.

http://sample.com:8088/Sample/index.jsp?keyname=test
async

Option to allow other threads to accessing the entry when a thread is updating the entry.

  • true : Entry is not blocked, and the requesting threads retrieve the value of the entry before the update. This is the default value.

  • false : Entry is blocked, and threads wait for the entry to be updated and retrieves the updated entry.

df

Deletes entries when overflow occurs. The number of entries that will be deleted can be set in the 'df' attribute as a ratio of 'size'.

The value is a real number where 0 <= factor <= 1.

For example, if the factor is 1, all cached entries are deleted. If the factor is 0, no more requests will be cached if there is an overflow.

7.2.3. Example of Using <jeus:cache>

This section shows how to use the <jeus:cache> tag attributes.

The following example compares the cached date with the current date when there is a request for the cache.jsp page.

[Example 7.2] Using <jeus:cache>: <<cache.jsp>>

<%@ taglib uri=”http://www.tmaxsoft.com/jeuscache” prefix=”jeus” %>
<HTML>
<BODY>
    Current time: <%= new Date() %><br>
    <jeus:cache timeout="60s"> 
        Cached time: <%= new Date() %>  
    </jeus:cache>
</BODY>
</HTML>

When the first request is made using the <jeus:cache> tag, the current date is displayed on the screen and the content will be cached in the JEUS WebCache. On the next request, the cached date will be displayed. After 60 seconds, an updated date will be displayed.

The following example shows how to use the <jeus:cache> tag when <jsp:include> is used to include other pages.


[Example 7.3] Using <jsp:include>: <<main.jsp>>

<HTML>
<BODY>
    <jsp:include page="cache.jsp"/>
</BODY>
</HTML>

The content of the <jeus:cache>tag in cache.jsp that is included in main.jsp displays the same result as the first example that only uses cache.jsp.


7.2.4. Using Flush

The flush tag forcibly deletes cached entries. Flush the entry using the 'name' attribute or the 'name' + 'key' attributes.

This example assumes that stock information is cached using the 'name' + 'key' attributes.

<jeus:cache name="stock" key="parameter.company" scope="application">
    . . . stock content . . .
</jeus:cache>

Use the following to delete the stock information that corresponds to the "parameter.company" key in the cache.

<jeus:cache name="stock" key="parameter.company" scope="application" flush=”true”/>

If flush is executed successfully as in the previous example, the updated stock content will be displayed when the stock information in the <jeus:cache> tag is requested. If only the 'name' attribute is used for the flush attribute, all entries that were saved with the "parameter.company" key will be deleted.

<jeus:cache name=“stock” scope=”application” flush=”true”/>

If the entries were cached by using only the 'name' attribute without the 'key' attribute, only the 'name' attribute can be used to flush the entries.

7.2.5. Using Refresh

If refresh is used, the body content will be created whenever <jeus:cache> tag is called.

This is not a <jeus:cache> tag attribute. It can be configured by setting '_jeuscache_refresh' to 'true'.

Use the following to refresh all entries in the application and session scopes.

<% application.setAttribute("_jeuscache_refresh", "true"); %>
<% session.setAttribute("_jeuscache_refresh", "true"); %>

This function checks the '_jeuscahe_refresh' value in each scope when the <jeus:cache> tag is accessed. If the value is true, it updates the body content. To disable refresh, set '_jeuscahe_refresh' to 'false'.

If the 'timeout' and 'flush' attributes are used, only updated parts of the entries are displayed. If refresh is used, all body contents of the scope are updated each time.

7.3. HTTP Response Caching

JEUS WebCache supports HTTP Response Caching function that caches the entire HTTP response by using servlet filters.

This method is not appropriate for pages are updated dynamically, but is useful for static contents such as image files and PDFs. This method can be used for web pages whose contents do not change or do not change often.

http://www.sample.com/filter/respcacheTest.jsp?key=value

As in the previous example, the entire URI that includes the key and value is used as the entry key. If the key or value are updated dynamically, the response for each URI will be cached.

Caution

Note that the HTTP response is only cached when the HTTP response state is 200 OK (HttpServletResponse.SC_OK). The URI is used as the entry key in JEUS WebCache.

This section describes how to apply HTTP response caching to web applications.

7.3.1. Configuring Filters

To use HTTP response caching, register a filter in web.xml.

The following example caches responses for all HTTP requests for which the <url-pattern> is '/filter/' for 10 minutes.

Note

Note that the jeus.servlet.cache.web.filter.CacheFilter class must be used as the <filter-class>.

[Example 7.4] Configuring Filters : <<web.xml>>

<web-app>
 . . .
<filter>
    <filter-name>CacheFilter</filter-name>
    <filter-class>jeus.servlet.cache.web.filter.CacheFilter</filter-class>
    <init-param>
        <param-name>timeout</param-name>
        <param-value>600</param-value>
    </init-param>
    <init-param>
        <param-name>lastModified</param-name>
        <param-value>on</param-value>
    </init-param>
    <init-param>
        <param-name>expires</param-name>
        <param-value>off</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>CacheFilter</filter-name>
    <url-pattern>/filter/*</url-pattern>
</filter-mapping>
 . . .
</web-app>


The following describe the initialization parameters that are sent to the filter class.

ParameterDescription
timeout

Timeout for caching HTTP responses. The default value is 3600 and the units are in seconds.

This is set in the same way as the 'timeout' attribute for JSP caching, except that HTTP response caching does not provide the flush function. Hence, if 'timeout' is set to '-1', the web application will not expire unless it is undeployed.

lastModified

Option to send the Last-Modified header in the HTTP response. This is used to reduce the load on the web engine.

The browser can ask the web engine if the cached contents have been updated since the last HTTP request. The web engine then compares the If-Modified-Since header information in the HTTP request with the Last-Modified time of the current entry. If there are no changes, the web engine sends an HTTP 302 code (HttpServletResponse.SC_NOT_MODIFIED).

Options are:

  • on: Determine the last modified time during filtering, and send an HTTP 304 code.

  • off: Do not send a 304 code as the HTTP response.

  • initial: Send a 304 code after setting the last modified time to the current time. This is the default value.

expires

Option to send the Expires header information in the response. If the browser uses caching, the cached contents are valid until they expire. They are used for subsequent HTTP requests.

However, if an entry in JEUS WebCache is updated, the new entry will conflict with the entry stored in the browser cache. In this case, set the Expires header information in the web engine to expire the content that is stored in the browser cache, and use the updated entry in the JEUS WebCache.

Options are:

  • on: If a value is set in the filter chain, send the Expires header.

  • off: Do not send the Expires header.

  • time: Add the previous time parameter value to the last-modified value of the HTTP response, set it in the Expires header, and send the response to the client.

Note

In addition to these parameters, the scope, size, async, and df parameters can also be configured. They are the same as those described in "7.2. JSP Caching".