Use oscache to improve program performance and oscache Program Performance
I. Introduction
Oscache is a widely used high-performance j2ee cache framework. It can be used in common cache solutions for any java application.
Ii. Features
1. Any object can be cached. You can cache some jsp pages or http requests without restriction. Any java object can be cached.
2. the cache can be permanently cached because the cache can be written to the hard disk at will.
3. clusters supported
4. You can control the expiration of cache objects.
Iii. Use
1. cache local pages
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri =" http://www.opensymphony.com/oscache "prefix =" cache "%> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 2. cache the entire page
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri =" http://www.opensymphony.com/oscache "prefix =" cache "%> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 3. cache object
public class CacheServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) {int refreshPeriod = 10; // cache in 6 secondsGeneralCacheAdministrator cache = new GeneralCacheAdministrator();try {// get data from cacheDate myDate = (Date) cache.getFromCache("myDate", refreshPeriod); System.out.println("current date is : " + myDate); } catch (NeedsRefreshException e) {try {Date myDate = new Date();// put data into cachecache.putInCache("myDate", myDate);} catch (Exception ex) {cache.cancelUpdate("myDate");}}}}
Oscache. properties
# The cache is cached in the memory by default. If it is set to false, the cache can only be cached in the database or hard disk. memory = false # persistent cache class. When this class is enabled, cache must be set. path information cache. persistence. class = com. opensymphony. oscache. plugins. diskpersistence. diskPersistenceListener # create a disk cache. path = D \: \ cache # Number of cache elements cache. capacity = 1000 # cache. cluster indicates the cluster settings. For example, # cache. cluster. multicast. ip is the broadcast ip address # cache. cluster. properties is the cluster attribute.
Web. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5"> <filter-name> CacheFilter </filter-name> <! -- CacheFilter only captures page requests with an Http header of 200, that is, only cache requests without errors --> <filter-class> com. opensymphony. oscache. web. filter. cacheFilter </filter-class> <init-param> <! -- Cache content time period, in seconds, the default value is 3600 seconds --> <param-name> time </param-name> <param-value> 10 </param-value> </init-param> <init-param> <param-name> scope </param-name> <param-value> application </param-value> </init-param> </filter> <! -- Cache all the cache. jsp file --> <filter-mapping> <filter-name> CacheFilter </filter-name> <url-pattern>/cache2.jsp </url-pattern> </filter-mapping> <servlet> <servlet-name> cacheServlet </servlet-name> <servlet-class> com. zdp. cache. cacheServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> cacheServlet </servlet-name> <url-pattern>/cacheServlet </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> cache1.jsp </welcome-file> </welcome-file-list> </web-app>
4. core API-GeneralCacheAdministrator:
1. void putInCache (String key, Object content); // put on Object in a cache
2. Object getFromCache (String key); // get on Object from the Cache
3. void removeEntry (String key); // remove an Object in a cache
4. void flushEntry (String key); // flushes a single cache entry
5. void flushAll (); // flush the entire cache immediately
6. void cancelUpdate (String key); // cacels a pending cache update
MATERIALS: http://www.cnblogs.com/huzi007/p/3873396.html