Oscache is currently the most widely used cache solution. JBoss, hibernate, spring, and so on are all supported,
The following describes how to configure and use Oscache.
1. Installation Process
Download the oscacheversion from http://www.opensymphony.com/oscache/download.html,
I downloaded the oscache-2.0.2-full version.
Decompress the downloaded file to the specified directory.
Get the Oscache. jar file from the extract directory and put it in the/WEB-INF/lib or the corresponding class library directory,
JAR file names may contain version numbers and release date information for this version, such as oscache-2.0.2-22Jan04.jar
If your JDK version is 1.3.x, we recommend that you add the commons-collections.jar package of Apache common lib to Lib.
If JDK is 1.4 or above, you do not need
Get the Oscache. properties file from the SRC or etc directory, put it in the SRC root directory or the/WEB-INF/classes directory of the release Environment
To create a disk cache, modify the cache. path information in Oscache. properties (remove the # note above ).
The win class path is similar to C: // app // cache.
The UNIX path is similar to/opt/MyApp/cache.
Copy the Oscache tag library file Oscache. TLD to the/WEB-INF/classes directory.
Now your application directory is similar to the following:
$ Web_application/WEB-INF/lib/Oscache. Jar
$ Web_application/WEB-INF/classes/Oscache. Properties
$ Web_application/WEB-INF/classes/Oscache. TLD
Add the following code to the Web. xml file:
<Taglib>
<Taglib-Uri> Oscache </taglib-Uri>
<Taglib-location>/WEB-INF/classes/Oscache. TLD </taglib-location>
</Taglib>
To facilitate debugging log output, you must add the commons-logging.jar and log4j-1.2.8.jar to the current class library path
Add the following two log output configuration files to the src directory:
The log4j. properties file contains the following content:
Log4j. rootlogger = debug, stdout, file
Log4j. appender. stdout = org. Apache. log4j. leleappender
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout
Log4j. appender. stdout. layout. conversionpattern = [start] % d {yyyy/mm/DD/HH: mm: SS} [date] % N % P [Priority] % N % x [NDC] % N % T [thread] n % C [category] % N % m [Message] % N % N
Log4j. appender. File = org. Apache. log4j. rollingfileappender
Log4j. appender. file. File = Oscache. Log
Log4j. appender. file. maxfilesize = 100kb
Log4j. appender. file. maxbackupindex = 5
Log4j. appender. file. layout = org. Apache. log4j. patternlayout
Log4j. appender. file. layout. conversionpattern = [start] % d {yyyy/mm/DD/HH: mm: SS} [date] % N % P [Priority] % N % x [NDC] % N % T [thread] n % C [category] % N % m [Message] % N % N
Log4j.logger.org. Apache. commons = Error
Log4j.logger.com. opensymphony. Oscache. base = info
Commons-logging.properties file content is
Org. Apache. commons. Logging. log = org. Apache. commons. Logging. impl. log4jcategorylog
2. Oscache. properties file Configuration Wizard
Cache. Memory
The value is true or false. The default value is cache in memory,
If it is set to false, the cache can only be cached in the database or hard disk. What is the significance of the cache :)
Cache. Capacity
Number of cache Elements
Cache. Persistence. Class
Persistent cache class. If this class is enabled, the cache. path information must be set.
Cache. Cluster Problems
Set Information for the cluster.
For example
Cache. Cluster. multicast. IP is the broadcast IP address.
Cache. Cluster. properties is the cluster attribute.
3. Basic usage of Oscache
The content of cache1.jsp is as follows:
<% @ Page import = "Java. util. *" %>
<% @ Taglib uri = "Oscache" prefix = "cache" %>
<HTML>
<Body>
Date not cached: <% = new date () %> <p>
<! -- Auto refresh -->
<Cache: cache time = "30">
Cache refresh date every 30 seconds: <% = new date () %>
</Cache: cache>
<! -- Manually refresh -->
<Cache: cache key = "testcache">
Manually refresh the cache Date: <% = new date () %> <p>
</Cache: cache>
<A href = "cache2.jsp"> manual refresh </a>
</Body>
</Html>
The following figure shows the manual refresh page for cache2.jsp execution:
<% @ Taglib uri = "Oscache" prefix = "cache" %>
<HTML>
<Body>
Cache refreshed... <p>
<Cache: flush key = "testcache" Scope = "application"/>
<A href = "cache1.jsp"> return </a>
</Body>
</Html>
You can also use the following statement to define the effective cache range. If the scope is not defined, the scope is applcation by default.
<Cache: cache time = "30" Scope = "session">
...
</Cache: cache>
4. cache filter cachefilter
You can define cache filters in Web. XML to define the cache of specific resources.
<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> 60 </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>
<URL-pattern> *. jsp </url-pattern>
</Filter-mapping>
The above definition caches all. JSP pages. The cache refresh time is 60 seconds, and the cache scope is session.
Note: cachefilter only captures page requests with an HTTP header of 200, that is, it only caches requests without errors,
Other requests (such as 500,404,400) are not cached.
Related Articles:
Use Oscache to improve J2EE system performance
Http://gceclub.sun.com.cn/yuanchuang/week-15/oscache.html
Http://www.cybercorlin.net/article.php? Story = 20041009121005707