Author: Xiao Jing
About the Author
Xiao Jing, software engineer, IBM DeveloperWorks / Bea dev2dev / sun technology developer, mainly research J2EE, Web Services and their implementation on WebSphere, WebLogic platform, with IBM's developing with WebSphere Studio certificates. You can get in touch with GuiLaida@163.com and the author, or view the author's home page for more information.
Thesis
Cache is a technique for improving system response speed, improving system operating performance. Especially in web applications, the system operation performance can be improved by the output of the cache page. This article gives you an open source project item for the Web application layer cache function in the J2EE framework ---- Oscache. By applying OSCache, we can not only realize the usual cache function, but also improve the stability of the system.
1
1.1 Special dynamic content that needs to be processed
We usually encounter such problems during the construction of the information system:
1. Changes in basic data
The content of the basic data that needs to be processed in the information system does not change in a short time, but in a relatively long period of time, it may be dynamically increased or decreased.
For example: the definition of the delivery area in e-commerce may not change in a short time, but with the expansion of the e-commerce business, the delivery area that needs to be processed in the system may increase. So our system has to display the delivery zone information each time and the database (assuming delivery zone information is saved in the database, this is also the usual processing method) to interact.
2. Question of statistical reports (not limited to statistical reports)
In general, statistical reports are a periodic job, probably half a month, one month or longer time will need to be updated once, but the statistical report is usually graphic or generating formats such as PDF, Word, Excel. Documents, these graphics content, the generation of files often require a lot of system resources, which causes a lot of burden to the system.
1.2 common point
By comparative analysis, it is not difficult to find that these two types have some common:
1. The treated content is short-time, so it can be treated as static content in a short time.
2. In a less time, the content being processed may or must change, so they must be processed as dynamic content.
3. It can ignore the impact after the change of content being processed in a reasonable time zone.
4. The processing action of these content is compared to system performance, affecting system response time.
1.3 solution
Cache technology can help us solve this problem very well:
1, cache information
When the above basic data or statistical report is first accessed, the processed content is treated as dynamic information, the base label is obtained from the database, and the statistics report will also generate a graphic, file that meets the requirements, and then this information will Put into the cache information.
2, the response information is provided by the cache
When the above basic data or statistics continue to be accessed, the system will first check if there is a corresponding content and our set cache rules in the cache information. If the cache information is in line with the caching rule, the response given from In cache information, if the cache information is not in line with the setting requirements, the system will repeat the action of the previous step.
Obviously, in step 2 above, in most cases, when the user requests arrives, the process will come from the cache, so much reduces the interaction with the database, or no longer need to generate a report for each request. Graphics or documents, the reduction in this part of this work is very beneficial for reducing system performance consumption, improving system stability and concurrency processing.
2 Introduction to OSCache
Oscache is a Cache technology for web application layers in the OpenSymphony organization to implement components, which resolves our problems. Oscache's current stable version is 2.0. The examples in this article are based on this version. If you have problems during the process of running examples, first confirm that the correct software version is used. 2.1 main features
1. Compatible with a variety of web servers support JSP
Web servers that have been compatible with Test include OrionServer (1.4.0 or above), Macromedia Jrun (3.0 or above), BEA WebLogic (7.x or above), IBM WebSphere (5.0 version), Silverstream (3.7.4 Version), Caucho Resin (1.2.3 or above), Tomcat (4.0 or above), other web servers that support servlet2.3, JSP1.2 should be fully compatible with OSCache.
2. Optional buffer
You can use memory, hard disk space, using memory and hard drives or provide your own other resources (you need your own adapter) as the cache.
Using memory as a cache will provide better performance Use a hard disk as a cache area to quickly recover cache content after the server will quickly recover the cache content Simultaneous use of memory and hard drives as a cache area, can reduce the occupation of memory.
3. Flexible cache system
OSCache supports caches some page content or response to page-level responses, and programmers can choose different cache levels depending on different needs.
4. Fault tolerance
In a general web application, if a page needs to be deal with the database, the web application and database cannot be interacted when the customer requests arrives, and the web application and database will be returned to the user "system error" or similar prompt information, if used If OSCache, you can use the cache to provide the user, win the maintenance system or take other remedies.
Other features include feature of the cluster, caching active refresh, and you can refer to other resources on the OpenSymphony website to get more information.
3 Installation of OSCache Components
Oscache is a web application-based component. His installation work is mainly configured for web applications, and approximately the steps are as follows:
1. Download, unzip oscache
Please go to Oscache's homepage http://www.opensymphony.com/oscache/download.html Download the latest version of OSCache, the author downloads the latest stable version 2.0 of OSCache.
Will download it. The zip file is decompressed to the C: / OSCache (% oscache_home% in the later chapter will use this directory) directory.
2. Newly established a web application
3. Place the main component% oscache_home / oscache.jar into the web-inf / lib directory
4. Treatment of Commons-Logging.jar, Commons-Collections.jar
The OSCache component uses Jakarta Commons Logging to handle log information, so you need to support the% OSCACHE_HOME% / LIB / CORE / COMMONS-Logging.jar (usually meant to put this file in Web- Inf / lib directory) If you use JDK1.3, put% oscache_home / lib / core / command-collections.jar into ClassPath, if you use JDK1.4 or above, you don't need it.
5. Add OSCACHE.PROPERTIES, OSCACHE.TLD to the web-inf / class directory
% Oscache_home% / oscache.properties contains setting information about OSCache Run feature values.% Oscache_home% / oscache.tld contains the definition of label libraries provided by OSCache 6. Modify web.xml files
Add the following content to the web.xml file, add support for the Taglib provided by OSCache:
taglib>
4 Start using cache components in OSCache
In Oscache, according to the different cache ranges into two different ways: one is part or all of the cached JSP pages, one is based on the cache of the entire page file.
4.1 JSP part content cache
4.1.1 Cache Tags provided by Cache-OSCache
This is the most important label provided by OSCache, including the content of the label to process the application cache mechanism, and the way the program will depend on the settings for the Cache tag properties.
When the first request arrives, the content in the tag is processed and cached. When the next request arrives, the cache system checks if the cache of this part has been invalid, mainly the following:
1. Cache time exceeds the timeout or duration property specified by the Cache tag settings 2. The time specified by the cron property is more late than the start time of the cache information. Other cache overdue settings
If any of the above four items, the cached content is considered to have failed, and the cached content will be reproced and returned to the process after the process, if the cached content is not invalid, then return to the user Will be the information in the cache.
Attribute description of the Cache tag:
Key - Identifies the keywords for cache content. There must be unique within the specified scope. The default key is the URI that is accessed and the request string behind it.
You can use a lot of cache tags in the same page without specifying his key properties, in which case the system uses the URI of this page and the following request string, and then automatically adds an index value to these keys to distinguish these cache content. But it is not recommended to use this way.
Scope - The range of cache effect can be Application or session
Time - The time period of the cache, the unit is second, the default is 3600 seconds, which is an hour, if a negative value is set, then this part of the cached content will never perform.
Duration - Specifies the time of failure of cache content, is another selection of relative time, you can use simple date format or compliant with the date format of USO-8601. Such as: duration = 'pt5m' duration = '5s', etc.
Refresh - false or true.
If the refresh property is set to True, this part of the cached content will be updated regardless of the other attributes, which gives the programmer a choice and decides when to be refreshed.
Mode - If the programmer does not want to be cached, you can set the mode attribute as "Silent" in the response to the user.
Other available properties include: Cron, Groups, Language, RefreshPolicyClass, RefreshPolicyParam.
The above attributes can be used alone or in combination as needed, and the following examples will explain the usage of these common properties.
4.1.2 Cache Label Instance Analysis:
The simplest cache tag usage
Use the default keyword to identify cache content, timeout is the default 3600 second
<%
// ourselves the JSP code content
%>
cache: cache>
2. Use the string specified to identify the cache content, and set the range of effects as session.
<%
// ourselves the JSP code content
%>
cache: cache>
3. Dynamically set the key value, use the timeout time specified by the TIME attribute to set the timeout time of the cache content, use the dynamic refresh value to determine whether to force the content refresh.
Because OSCache uses the key value to identify cache content, using the same Key value will be considered using the same cache content, so using dynamic Key values can freely depending on different roles, different requirements decide to use different cache content.
<%
// ourselves the JSP code content
%>
cache: cache>
4. Set the TIME attribute as a negative number to make the cache content never expire
<%
// ourselves the JSP code content
%>
5. Use the DURATION property to set up time
<%
// ourselves the JSP code content
%>
6. Use the MODE attribute to make the cached content to the customer's response
<%
// ourselves the JSP code content
%>
4.2 Implementing page-level cache with Cashfilter
A cachefilter is provided in the OSCache component to implement a cache for page-level, mainly to cache some dynamic pages in web applications, especially those that need to generate PDF format files / reports, images, etc., not only reduction The interaction of the database, reducing the pressure of the database server, and has a significant effect on reducing the performance consumption of the web server.
This function is implemented by configuring the cache in web.xml, and can also set the cache related properties, this configuration file-based implementation method should be a standard for J2EE. The implementation is implemented.
[Note] Only page information of the code 200 (that is, access has been successful) can only be cached when only customer access
Cache a single file
Modify Web.xml, add the following, determine the cache to the /TestContent.jsp page.
filter>
filter-mapping> 2. Cache URL Pattern
Modify Web.xml, add the following, determine the * .jsp page to cache.
filter>
filter-mapping>
3. Set your own cache properties
In the case of page-level cache, you can determine some of the characteristics of the cachefilter: Time property Set the time period of the cache, the default is 3600 seconds, you can set only the settings according to your own needs, and scope property settings, default For Application, optional options include Application, Session
init-param>
init-param>
filter>
filter-mapping>
5 Performance test results
5.1 Test Environment
System Platform: Windows 2000 Advanced Server / P3 800 / 512M Memory
Web server: WebSphere 5.0
Database server: mysql 4.0.18-nt
Performance Test Tools: Apache Jmeter
5.2 Test Plan
This performance test contrast is two kinds of cache and do not use cache, their access code is the same: through the data source from the local mysql database, all records of the Person table, and display it on the page.
10 users will be imitated in the test, and each user initiates 5 requests, and then all the time spending all access.
5.3 test results
Test results when using the cached test result do not use the cache
Total time for all requests (ms) 20569 22870
Detailed results of Performance Test, please check out "System Performance Test Results when using Cache" in Download Content .TXT "and" Use Cache System Performance Test Results .txt "
6 summary
In the J2EE system, we often need to deal with some special dynamic content, which is very limited in a time period, but they have to determine them to operate as dynamic content, and very consume database system resources or web servers. Resources, when we can use Cache - a technology for improving system response speed, improve system operating performance - to optimize our system. Especially in web applications, this processing can improve system operating performance. This article gives you an open source project item for the Web application layer cache function in the J2EE framework ---- Oscache. It provides a rich feature that enables cache needs in the J2EE system. By applying OSCache, we can not only realize the usual Cache functionality, free setting Cache's related features such as cache time segments / cache content, etc., improve system performance, and effectively improve the stability of the system. In addition, the OSCache component also provides more features such as clusters, fault tolerance, flexible cache selection.
According to your own experience, they provide some simple examples. They demonstrate how to use the rich features provided by the OSCache component, and the features provided by Oscache are far more than this. They need to be in-depth research in the future time, and hope Everyone has contributed research results through E-mail and the author.
Reference
OpenSymphony website About OSCACHE HTTP: //www.opensymphony.com/oscache/
Oscache download address http://www.opensymphony.com/oscache/download.html
Performance test results.rar programming