Table of Contents
This chapter describes how to use JEUS WebCache to improve the web application performance.
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.
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.
JSP caching improves the performance of web applications by storing parts of JSP pages in JEUS WebCache by using the JSP tag library.
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.
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.
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.
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.
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.
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.
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.
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.
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 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.
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".