Using Buffer technology to improve the performance and stability of JSP programs (1)

Source: Internet
Author: User

I. Overview

In Web applications, the generation of some reports may take a long time for the database to calculate. Some websites provide weather information, which requires access to a remote server for SOAP calls to obtain temperature information. All of these are examples of complex information. Adding too much complex information to the Web page may cause excessive load on the Web server and database server. The buffer of JSP code blocks gives developers the freedom to add various complex information at will.

JSP can encapsulate and run complex Java code in the tag library, making it easier to maintain JSP page files, making it easier for non-professional developers to use JSP page files. There are already many tag libraries, either commercial products or open source code products. However, most of these products only use the tag library to implement functions that can be implemented with a simple Java Scriptlet. Few products use custom tags in a creative way, it provides usage that is almost impossible to implement before the JSP custom tag library appears.

The OSCache tag library is designed by OpenSymphony. It is a groundbreaking JSP custom tag application that provides fast memory buffering within the existing JSP pages. Although some vendors are already providing various types of cache products, they all belong to specific vendors. OSCache can run on any JSP 1.1 compatible server. It can not only buffer existing JSP code blocks for all users, but also buffer them by users. OSCache also includes some advanced features to improve scalability, such as buffering to disks, programmable buffer refreshing, exception control, and so on. In addition, as with other OpenSymphony products, OSCache code is also released free of charge under an open source code license agreement.

This article takes the design process of a hypothetical auction website as an example to introduce the working process of OSCache. This hypothetical Web site will include: a management page for reporting recent auction events; a fully functional home page with various promotional information; A special navigation bar, it contains information about all the unsold auction activities of the user.

Ii. Management page

The auction website contains a management report. It takes several seconds for the database server to create such a report. It is important to generate a report for a long time, because we may allow multiple administrators to monitor system running conditions and avoid the administrator from generating the report every time he accesses the report. To achieve this, we encapsulate the entire page into an application-level buffer tag, which is refreshed every hour. Some products provided by other vendors also have similar features, but OSCache is better than them.

For the sake of simplicity, we will not focus much on the format issue. When writing the management page, we first add the tag library declaration to the page:

<%@ taglib uri="cachetags" prefix="cache" %>
Next we will use the cache tag to enclose the entire page. The default cache duration is one hour.
<Cache: cache>... complex management reports... </cache: cache>

The management page is buffered. If the Administrator accesses the same page again within one hour after the page is generated, he will see the previously cached page, and the database server does not need to generate this report again.

3. Home Page

The home page of the auction website displays the activity information of the website and promotes the upcoming auction activities. We want to display the number of ongoing auction activities, the number of currently logged-on users, the list of auction activities that will end in the short term, and the current time. This information has different time precision requirements. The auction activity on the website usually lasts for several days, so we can set the buffer effective auction activity quantity to 6 hours. The number of users is obviously changing frequently, but here we will buffer this value for 15 minutes each time. Finally, we hope that the current time displayed on the page will always be the exact page access time.

After declaring the tag library on the homepage, we first output the current date directly without buffering:

Now: <% = new java. util. Date () %>

Next, we will display a list of auction activities that will end in the short term:

<Cache: cache> <ul> <% // construct an Iterator auctions = .... while (auctions. hasMore () {Auction auction = (Auction) auctions. next (); %> <li> <% = auction %> </li % <}%> </ul> </cache: cache>

Finally, we want to display the number of ongoing auction activities. This number needs to be buffered for 6 hours. Because the cache tag requires the number of seconds to buffer data, we convert 6 hours to 21600 seconds:

<Cache: cache time = "21600"> <% // query the database to obtain the total number of au events int auctionCount = .... %> There are <% = auctionCount %> ongoing au events on this website! </Cache>

We can see that we only use a small amount of code to construct a home page with a complex buffer system. This buffer system caches each part of the page separately, and the buffer time of each part fully complies with the frequent changes of their respective information. With buffering, we can add more content to the home page. If there is no Buffering in the past, too much content on the home page will lead to slow page access, it may even overload the database server.


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.