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.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.
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.
The 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.
The scope of the cached entries. Options are 'application' or 'session'. (Default value: application)
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.
The 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.
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
Option to allow other threads to accessing the entry when a thread is updating the entry.
Value | Description |
---|---|
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. |
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. (Default value: 0.25)
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".