Reprinted from http://www.javaeye.com/topic/172186
Author thblovezhj
Oscache
Is currently the most widely used cache solution, JBoss, hibernate, spring
And so on,
The following is a brief introduction to Oscache.
Configuration and use process.
1. Installation Process
From http://www.opensymphony.com/oscache
/Download.html download the appropriate Oscache
Version,
I downloaded Oscache.
-2.0.2-full.
Decompress the downloaded file to the specified directory.
Obtain Oscache from the extract directory
Put the. jar file in the/WEB-INF/lib or the corresponding class library directory,
The jar file name may contain the version number and the release date of the 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 Oscache from SRC or etc directory
. Properties file, put in the SRC root directory or the/WEB-INF/classes directory of the release Environment
To create a disk cache, you must modify the Oscache
Cache. path information in. 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 Oscache
Tag library file Oscache
. TLD to/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
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
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. Oscache
Basic usage
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.