Application of OSCache in J2EE System with High Concurrent Data Volume

    Enterprise application server WebJSPCache
    In the previous article, I wrote that timers are not very real-time for high-concurrency processing. I originally wanted to write memcache applications, but I found a well written description of OSCache. Very good write Yes , the excerpt is as follows:


Application of OSCache in J2EE system
Abstract
Cache is a technology used to improve system response speed and system performance. Especially in Web applications, the system performance can be significantly improved by caching the output of the page. In this article, the author will introduce OSCache, an open source project that implements the caching function of the Web application layer in the J2EE framework. By applying OSCache, we can not only achieve the usual Cache function, but also improve the stability of the system.

1 Problems faced


1.1 Special dynamic content that needs to be processed


In process of information system construction, we usually encounter such problems:

1.

Change of basic data The content of basic data that needs to be processed in the information system will not happen in a short time. It varies, but it may increase or decrease dynamically over a relatively long period of time.

For example: the definition of delivery area in e-commerce may not change in a short time, but with the expansion of e-commerce business, the delivery area that needs to be processed in the system may increase. Therefore, our system has to interact with the database every time the delivery area information is displayed to the customer (assuming the delivery area information is stored in the database, which is also the usual processing method).



2. Problems with statistical reports (not limited to statistical reports)

Generally speaking, the statistical report is a periodic work, and it may be updated once every half a month, a month or longer. However, the statistical report is usually displayed in graphics or in the form of pdf, word, excel, etc. The generation of these graphics contents and files usually consumes a lot of system resources, which causes a great burden to the system operation.

1.2 Common points of problems


Through comparative analysis, it is not difficult to find that these two types of problems have some common points:

1. The content to be processed does not change for a short time, so it can be processed as static content in a short time.

2. In a not too long During the time period, the processed content may or will change, so they must be processed as dynamic content.

3. The impact of changes in the processed content can be ignored within a reasonable time period.

4. Actions for processing these content. It consumes system performance and affects system response time

. 1.3 Solution


Caching technology can help us solve this problem very well:

1. Cache information

When the above basic data or statistical reports are accessed for the first time, the processed content is treated as Dynamic information, the basic data library is obtained from the database, and the statistical reports will also be generated to meet the requirements of graphics and files, and then these information will be put into the cache information.

2. The response information is provided by the cache.

When the above-mentioned basic data or statistical reports continue to be accessed, the system will first check whether there is corresponding content in the cached information and the caching rules we set. If the cached information exists and conforms to the caching rules , the given response will come from the cached information. If there is no cached information or the cached information does not meet the set requirements, the system will repeat the action of the previous step.

Obviously, in the above step 2, in most cases, when the user request arrives, the processed content will come from the cache, so the interaction with the database is greatly reduced, or it is no longer necessary to generate a report for each request. Graphics or files, the reduction of this part of the work is very beneficial to reduce system performance consumption, improve system stability and concurrent processing capabilities.

2 OSCache Introduction


OSCache is a cache technology implementation component of the Web application layer in the J2EE architecture provided by the OpenSymphony organization. Its appearance solves the problems we face. The latest stable version of OSCache is 2.0. The examples in this article are based on this version. If you encounter problems when running the examples, please first confirm whether the correct software version is used.

2.1 Main features


1. Compatible with a variety of web servers that support JSP Web servers

that have passed the compatibility test include OrionServer (version 1.4.0 or above), Macromedia JRun (version 3.0 or above), BEA Weblogic (version 7.x or above) , IBM Websphere (version 5.0), Silverstream (version 3.7.4), Caucho Resin (version 1.2.3 or above), Tomcat (version 4.0 or above), and other web servers that support servlet2.3 and jsp1.2 should all be Fully compatible with OSCache.

2. Optional buffer area

You can use memory, hard disk space, use memory and hard disk at the same time, or provide your own other resources (you need to provide your own adapter) as a buffer area.

Using memory as a cache area will provide better performance
. Using a hard disk as a cache area can quickly restore the cached content after the server is restarted.
Using both memory and hard disk as a cache area can reduce memory usage
3. Flexible caching system

OSCache supports caching of partial page content or page-level response content. Programmers can choose different cache levels according to different needs and different environments.

4. Fault tolerance

In general web applications, if a page needs to deal with the database, and when the client request arrives, the web application and the database cannot interact, then a "system error" or similar prompt information will be returned to the user , if OSCache is used, you can use the cache to provide users with time to maintain the system or take other remediation.

Other features include support for clusters, active cache refresh, etc. You can refer to other resources on the OpenSymphony website for more information.

3 Installation of OSCache components


OSCache is a component based on web applications. Its installation work is mainly to configure web applications. The general steps are as follows:

1. To download and decompress OSCache,

please go to the homepage of OSCache http://www.opensymphony .com/oscache/download.html http://www.bt285.cn/content.php?id=1196863 http://www.5a520.cn


to download the latest version of Oscache, the author downloaded the latest stable version 2.0 of OSCache .

will be downloaded. Extract the Zip file to c:oscache (in the following chapters will use %OSCache_Home% to represent this directory) directory

2. Create a new web application

3. Put the main component %OSCache_Home%oscache.jar into the WEB-INFlib directory

4 . Processing of commons-logging.jar and commons-collections.jar

The OSCache component uses Jakarta Commons Logging to process log information, so it needs the support of commons-logging.jar, please put %OSCache_Home%libcorecommons-logging.jar into the classpath (usually means put this file into the WEB-INFlib directory)
If you use JDK1.3, please put %OSCache_Home%libcorecommons-collections.jar into the classpath, if you use JDK1.4 or above, it is not required
5. Put oscache.properties and oscache.tld into the WEB-INFclass directory

%OSCache_Home% oscache.properties contains the setting information for OSCache operating characteristic values
​​%OSCache_Home% oscache.tld contains the definition content of the tag library provided by OSCache
6. Modify the web.xml file Add the following content

to the web.xml file to increase the support for OSCache Provided taglib support:

<taglib>
            <taglib-uri>oscache</taglib-uri>
            <taglib-location>/WEB-INF/classes/oscache.tld</taglib-location>
            </taglib>



4 Getting started with OSCache Cache component in


OSCache is divided into two different ways according to the different cache scope: one is to cache part or all of the content in the JSP page, and the other is to cache based on the entire page file.

4.1 JSP partial content cache


4.1.1 Cache tag provided by Cache-OSCache


This is the most important tag in the tag library provided by OSCache. The content included in the tag will be processed by the caching mechanism, and the processing method will depend on the programmer The setting for the cache tag attribute.

When the first request arrives, the content in the tag is processed and cached. When the next request arrives, the caching system will check whether the cache of this part of the content has been invalidated, mainly the following items:

1. The cache time exceeds the cache tag The timeout time specified by the set time or duration attribute
2. The time specified by the cron attribute is later than the start time of the cache information
3. The cached content in the tag is refreshed after being cached
4. Other cache timeout settings
If the above are met For any of the four items, the cached content is considered to have been invalidated. At this time, the cached content will be reprocessed and the processed information will be returned. If the cached content has not been invalidated, the cached content will be returned to the user. information in .

Attribute description of the cache tag:

key - The keyword that identifies the cached content. Must be unique within the specified scope. The default key is the URI of the page being accessed and the following request string.

You can use many cache tags in the same page without specifying its key attribute. In this case, the system uses the URI of the page and the following request string, and automatically adds an index value to these keys to distinguish these caches content. But this method is not recommended.

scope - the scope in which the cache takes effect, which can be application or session

time - The time period for the cached content, in seconds, the default is 3600 seconds, which is one hour. If a negative value is set, then this part of the cached content will never expire.

duration - specifies the time when the cached content expires. It is another option relative to time. It can use a simple date format or a date format that conforms to USO-8601. Such as: duration='PT5M' duration='5s', etc.

refresh - false or true.

If the refresh attribute is set to true, this part of the cached content will be updated regardless of whether other attributes are eligible, giving the programmer a choice as to when a refresh must be made.

mode - If the programmer does not want the cached content to be added to the response to the user, the mode attribute can be set to "silent".

Other available attributes include: cron, groups, language, refreshpolicyclass, refreshpolicyparam.

The above properties can be used alone or in combination as needed. The following examples will explain how these common properties are used.

4.1.2 Cache tag instance analysis:


1. The simplest cache tag usage

Use the default keyword to identify the cache content, the timeout time is the default 3600 seconds

<cache:cache>
            <%
            //Your own JSP code content
            %>
            < /cache:cache>



2.

<cache:cache key="foobar" scope="session">
            <%
            //your own JSP code content
            %>
            </cache:cache>



3. Dynamically set the key value, use the time attribute specified by yourself to set the cache content The timeout period, use the dynamic refresh value to decide whether to force the content to refresh.

Because OSCache uses the key value to identify the cached content, using the same key value will be considered to use the same cached content, so using the dynamic key value can freely decide to use different cached content according to different roles and different requirements.

<cache:cache key="<%= product.getId() %>" time="1800" refresh="<%= needRefresh %>">
            <%
            //your own JSP code content
            %>
            </cache:cache >



4. Set the time attribute to a negative number to make the cached content never expire

<cache:cache time="-1">
            <







<cache:cache duration='PT5M'>
            <%
            //your own JSP code content
            %>



6. Use the mode attribute to prevent the cached content from being added to the client's response

<cache:cache mode='silent'>
            <%
            //Your own JSP code content
            %>



4.2 Implement page-level caching with CashFilter A CacheFilter is provided


in the OSCache component to implement page-level caching, mainly for caching some dynamic pages in web applications, especially those Pages that need to generate pdf files/reports, picture files, etc., not only reduce the interaction of the database, reduce the pressure on the database server, but also have a significant effect on reducing the performance consumption of the web server.

The implementation of this function is to decide which page or group of pages to cache by configuring in web.xml, and also to set the relevant properties of the cache. This configuration file-based implementation should be a standard for J2EE. way of implementation.

[Note] Only the page information with the code 200 in the http header (that is, the access has been successful) can be cached when the client

accesses

cache.

<filter>
            <filter-name>
            <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
            </filter>
            <filter-mapping>
            <filter-name>CacheFilter</filter-name>
            <!-right/testContent. Cache the content of the jsp page -->
            <url-pattern>/testContent.jsp</url-pattern>
            </filter-mapping>



2. Cache the URL pattern

Modify web.xml, add the following content, and determine the *.jsp page cache.

<filter>
            <filter-name>CacheFilter</filter-name>
            <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
            </filter>
            <filter-mapping>

            <!-Cache all jsp page content-->
            <url-pattern>*.jsp</url-pattern>
            </filter-mapping>



3. Set the cache properties by yourself

In the case of page-level caching, you can pass Set the initial properties of CacheFilter to determine some features of the cache: the time property sets the time period of the cache, the default is 3600 seconds, which can be set according to your own needs, and the scope property is set, the default is application, and the options include application, session

< filter>
            <filter-name>CacheFilter</filter-name>
            <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
            <init-param>
            <param-name>time</param -name>
            <param-value>600</param-value>
            </init-param>
            <init-param>
            <param-name> scope</param-name>
            <param-value>session</param-value>
            </init-param>
            </filter>
            <filter-mapping>
            <filter-name>CacheFilter</filter-name>
            <!-cache all jsp page content- ->
            <url-pattern>*.jsp</url-pattern>
            </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 tool: apache Jmeter

5.2 test plan


This performance test compares the use of cache and no cache, and their access codes are the same: get from the local mysql database through the data source All records of the person table are then displayed on the page.

In the test, 10 users will be imitated, and each user will initiate 5 requests, and then the time spent on all visits will be counted.

5.




The total time spent by all requests (ms) 20569 22870 For

the detailed results of the performance test, please check the "System Performance Test Results Without Cache.txt" and "System Performance Test Results After Using Cache.txt" in the download content.

6 Summary


In the J2EE system, we often need to deal with some special dynamic content. The changes of these content in a period of time are very limited, but they have to be determined as dynamic content for output, and they consume a lot of database system resources or web server resources. resources, then we can use Cache—a technology used to improve system response speed and system performance—to optimize our system. Especially in Web applications, this kind of processing can significantly improve system performance.

In this article, the author will introduce OSCache, an open source project that implements the caching function of the Web application layer in the J2EE framework. It provides rich functionality needed to implement caching in J2EE systems. Through the application of OSCache, we can not only realize the usual Cache function, and freely set the relevant characteristics of the cache, such as the cache time period/cache content, etc., to improve the system performance, but also effectively improve the stability of the system. In addition, the OSCache component also provides more features such as clustering, fault tolerance, flexible cache selection, etc.

The author provides you with some simple examples based on their own experience. They partially demonstrate how to use the rich features provided by the OSCache component. The features provided by OSCache are far more than these, and you need to do in-depth research in the future.
This article comes from: http ://www.blogjava.net/zhongli/archive/2009/02/22/256033.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326940267&siteId=291194637