) Oscache usage

Source: Internet
Author: User
Tags tld ibm developerworks
Copyright Disclaimer: csdn is the hosting service provider of this blog. If this article involves copyright issues, csdn does not assume relevant responsibilities. Ask the copyright owner to contact the author directly.

Author: Xiao Jing

Author Profile

Xiao Jing, software engineer, IBM developerworks/BEA dev2dev/Sun technology developer writer, focuses on J2EE and web services and their implementation on WebSphere and WebLogic platforms, has the IBM developing with WebSphere Studio certificate. You can contact the author through the guilaida@163.com or view the author's home page for more information.

Abstr

Cache is a technology used to improve system response speed and system running performance. Especially in Web applications, caching page output results can significantly improve system performance. This article introduces Oscache, an open-source project that implements the web application layer cache function in the J2EE framework. By applying Oscache, we can not only implement the common cache function, but also improve the system stability.

1. Problems

1.1 special dynamic content to be processed

During information system construction, we usually encounter the following problems:

1. Basic data changes

The content of the basic data to be processed in the Information System will not change in a short period of time, but it may increase or decrease dynamically in a relatively long period of time.

For example, the definition of the shipping area in e-commerce may not change in a short period of time, but with the expansion of e-commerce enterprise business, the shipping area to be processed in the system may increase. Therefore, our system has to interact with the database every time it shows the delivery region information to the customer (assuming that the delivery region information is saved in the database, which is also a common solution.

2. Statistical Reports (not limited to statistical reports)

Generally, a statistical report is a periodic task. It may take half a month, a month, or longer to be updated, however, statistical reports are usually used to display images or generate files in PDF, Word, Excel, and other formats. The generation of these images and files usually consumes a lot of system resources, it causes a great burden on system operation.

1.2 commonalities

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

1. the content to be processed remains unchanged for a short time, so it can be processed as static content in a short time.

2. In a very short period of time, the content to be processed may or will inevitably change. Therefore, they must be processed as dynamic content.

3. You can ignore the impact of content changes in a reasonable time range.

4. Processing the content consumes system performance and affects system response time.

1.3 Solution

The cache technology can help us solve this problem well:

1. Cache Information

When the preceding basic data or statistical report is accessed for the first time, the processed content is treated as dynamic information, and the basic data base is obtained from the database, statistical reports will also generate images and files that meet the requirements, and the information will be stored in the cache information.

2. The response information is provided by the cache

When the above basic data or statistical report is accessed, the system will first check whether the cache information contains the corresponding content and the cache rules we set, if the cached information exists and complies with the cache rules, the response will come from the cache information. If no or the cache information does not meet the set requirements, the system will repeat the previous action.

Obviously, in step 2 above, when a user request arrives, the content to be processed will come from the cache, greatly reducing the interaction with the database, or you no longer need to generate a report image or file for each request. Reducing this part of work is very helpful for reducing system performance consumption, improving system stability and concurrent processing capabilities.

2 Oscache Overview

Oscache is a cache technology implementation component of the Web application layer in the J2EE architecture provided by the opensymphony organization. Its emergence solves our problems. Currently, the latest stable version of Oscache is 2.0. The examples in this article are based on this version. If any problem occurs during the running of the example, check whether the correct software version is used.

2.1 Main Features

1. compatible with a variety of web servers supporting JSP

Web servers that have passed the compatibility test include orionserver (1.4.0 or later), Macromedia JRun (3.0 or later), BEA WebLogic (7. X or later), IBM WebSphere (5.0), SilverStream (3.7.4), Caucho Resin (1.2.3 or later), Tomcat (4.0 or later ), other Web servers that support servlet2.3 and jsp1.2 should be fully compatible with Oscache.

2. Optional cache area

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

  • Using memory as a cache area will provide better performance
  • Using a hard disk as a cache area can quickly restore cached content after the server restarts
  • Using both the memory and hard disk as the cache area can reduce memory usage.

3. Flexible Cache System

Oscache supports caching part of the page content or page-level response content. programmers can select different cache levels based on different needs and environments.

4. Fault Tolerance

In a general web application, if a page needs to deal with a database, and when the customer requests arrive, the Web application and the database cannot interact, the system error message or similar prompt information will be returned to the user. If Oscache is used, you can use the cache to provide it to the user, give yourself time to maintain the system or take other remedial measures.

Other features include cluster support and active cache refreshing. For more information, see other resources on the opensymphony website.

3. Install the Oscache component

Oscache is a Web application-based component. Its installation is mainly to configure web applications. The steps are as follows:

1. Download and decompress Oscache

Go to the Oscache Homepage.

Will be downloaded. Decompress the ZIP file to the C:/Oscache directory (% oscache_home % will be used to represent this directory in the subsequent sections ).

2. Create a new Web Application

3. Put the main component % oscache_home %/Oscache. jar into the WEB-INF/lib directory

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

  • The Oscache component uses Jakarta commons logging to process log information, so commons-logging.jar support is required, put % oscache_home %/lib/CORE/commons-logging.jar into classpath (which usually means to put this file into the WEB-INF/lib directory)
  • If JDK is used, put % oscache_home %/lib/CORE/commons-collections.jar into classpath. If JDK or later is used, no more

5. Put Oscache. properties, Oscache. TLD into the WEB-INF/class directory

  • % Oscache_home %/Oscache. properties contains settings for the running feature values of Oscache.
  • % 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 support the taglib provided by Oscache:

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

4. Start using the cache component in Oscache

In Oscache, there are two different caching Methods: one is to cache part or all of the content in the JSP page, and the other is based on the cache of the entire page file.

4.1 JSP part content Cache

4.1.1 cache-Cache tags provided by Oscache

This is the most important tag in the tag library provided by Oscache, including processing the application cache mechanism for the content in the tag. The processing method depends on the programmer's setting of the cache tag attribute.

When the first request arrives, the content in the tag is processed and cached. When the next request arrives, the cache system checks whether the cache for this part of content has expired, mainly including the following items:

  • 1. the cache time exceeds the time specified by the cache tag or the timeout time specified by the Duration Attribute.
  • 2. the cron attribute specifies a later time than the start time of the cache information.
  • 3. the cached content in the tag is refreshed again after being cached.
  • 4. Other cache expiration settings

If any of the preceding four items is met, the cached content is deemed to have expired. The cached content will be reprocessed and the processed information will be returned, if the cached content does not expire, the information returned to the user will be the information in the cache.

Attributes of the cache Tag:

Key-Keywords used to identify the cached content. It must be unique within the specified scope. The default key is the uri of the accessed page and the subsequent request string.

You can use a lot of cache tags on the same page without specifying its key attribute. In this case, the system uses the URI of the page and the subsequent request string, in addition, an index value is automatically added to these keys to distinguish the cached content. However, this method is not recommended.

Scope-The cache scope can be application or session.

Time-The cache content time period, in seconds. The default value is 3600 seconds, which is an hour. If a negative value is set, the cached content will never expire.

Duration-Specifying the cache content expiration time is another option for relative time, you can use a simple date format or a date format that complies with the USO-8601. For example, duration = 'pt5m 'duration = '5s'

Refresh-False or true.

If the refresh attribute is set to true, the cached content will be updated no matter whether other attributes meet the conditions. This gives the programmer an option to decide when to refresh the content.

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

Other available attributes include Cron, groups, language, refreshpolicyclass, and refreshpolicyparam.

The preceding attributes can be used independently or in combination as needed. The following example describes how to use these common attributes.

4.1.2 Analysis of cache tag instances:

1. The simplest cache tag usage

The default keyword is used to identify the cache content. The timeout time is the default 3600 seconds.

<Cache: cache> <% // your JSP code content %> </cache: cache>

2. Use the specified string to identify the cached content and set the scope to session.

<Cache: cache key = "foobar" Scope = "session"> <% // your JSP code content %> </cache: cache>

3. dynamically set the key value, set the cache content timeout time using the time attribute you specified, and use the Dynamic Refresh value to determine whether to force content refresh.

Because Oscache uses the key value to identify the cached content, using the same key value will be considered to use the same cache content, therefore, dynamic key values allow you to use different cached content based on different roles and 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 value so that the cached content never expires.

<Cache: cache time = "-1"> <% // your JSP code content %>

5. Use the Duration Attribute to set the expiration time

<Cache: cache duration = 'pt5m '> <% // your JSP code content %>

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

<Cache: cache mode = 'silent'> <% // your JSP code content %>

4.2 Use cashfilter to implement page-level cache

The Oscache component provides a cachefilter for page-level caching. It is mainly used to cache some dynamic pages in Web applications, especially for pages that need to generate PDF files/reports and image files, this not only reduces database interaction, but also reduces the pressure on database servers, in addition, it significantly reduces the performance consumption of web servers.

This function is implemented through the Web. configure in XML to decide which page or group of pages to cache, and you can also set cache-related attributes, this configuration file-based implementation method should be a standard implementation method for J2EE.

[Note] only the page information with code 200 (successful access) in the HTTP header information returned by the customer can be cached

1. cache a single file

Modify web. xml and add the following content to determine whether to cache the/testcontent. jsp page.

<Filter> <filter-Name> cachefilter </filter-Name> <filter-class> COM. opensymphony. oscache. web. filter. cachefilter </filter-class> </filter> <filter-mapping> <filter-Name> cachefilter </filter-Name> <! -Cache the content on the/testcontent. jsp page --> <URL-pattern>/testcontent. jsp </url-pattern> </filter-mapping>

2. cache URL Pattern

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

<Filter> <filter-Name> cachefilter </filter-Name> <filter-class> COM. opensymphony. oscache. web. filter. cachefilter </filter-class> </filter> <filter-mapping> <filter-Name> cachefilter </filter-Name> <! -Cache all JSP page content --> <URL-pattern> *. jsp </url-pattern> </filter-mapping>

3. Set cache attributes by yourself

In the case of page-level cache, you can set the initial attribute of cachefilter to determine some of the cache features: the time attribute sets the cache time period, which is 3600 seconds by default, you can only set the scope Attribute Based on your needs. The default scope attribute is application. Optional options include application and 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/512 M memory

Web server: WebSphere 5.0

Database Server: MySQL 4.0.18-NT

Performance Testing Tool: Apache jmeter

5.2 Test Plan

In this performance test, the comparison is to use the cache or not use the cache. Their access code is the same: retrieve all records of the person table from the local MySQL database through the data source, and then displayed on the page.

In the test, 10 users will be imitated, and each user will initiate 5 requests and then count the time spent on All accesses.

5.3 test results

Test results when cached test results are not cached

Total time spent on all requests (MS) 20569 22870

Else

6. Conclusion

In the J2EE system, we often need to deal with some special dynamic content. The changes to these content within a period of time are very limited, but they have to be identified as dynamic content for output, in addition, it consumes a lot of database system resources or web server resources. In this case, we can use cache-a technology used to increase the system response speed and improve the system running performance-to optimize our system. Especially in Web applications, this processing can significantly improve the system running performance.

This article introduces Oscache, an open-source project that implements the web application layer cache function in the J2EE framework. It provides a variety of functions required to implement caching In the J2EE system. By applying Oscache, we can not only implement common cache functions, but also freely set cache-related features, such as cache time periods/cache content, to improve system performance, it can also effectively improve the system stability. In addition, the Oscache component provides more features such as cluster, fault tolerance, and flexible cache selection.

The author provides some simple examples based on his own experience. They demonstrate how to use the rich features provided by the Oscache component, we need to study it in depth in the future, and hope that you can make contributions through email and the author.

References

Some http://www.opensymphony.com/oscache/ about Oscache on the opensymphony website

Http://www.opensymphony.com/oscache/download.html Oscache

Performance Test Result .rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.