OSCache使用经验总结

OSCache使用经验总结
转自 http://www.blogjava.net/jackstudio/archive/2006/09/08/68569.html                          
OSCache的使用主要有4种:
POJO 缓存
HTTP Response 缓存
JSP Tag Library 缓存
O/R Data Access 缓存

1、POJO 缓存
这种方式的缓存直接调用OSCache的API进行,主要用于处理页面内容会根据参数动态改变,可以将参数设置为key值来保存数据:
首先,声明成员变量:
 // OSCache Adminitrator instance
 private static GeneralCacheAdministrator cacheAdmin = null;
其次,进行初始化:
 public RingArtistAction() {
  cacheAdmin = new GeneralCacheAdministrator();
 }
将POJO进行缓存:
  // Cache data key and refresh period
  String key = sex + ":" + place;
int refreshPeriod=Constants.getIntegerValue(Constants.OSCACHE_REFRESH_PERIOD).intValue();
  try {
      // Get from the cache
   artists = (Map) cacheAdmin.getFromCache(key, refreshPeriod);
  } catch (NeedsRefreshException nre) {
      try {
          // Get the value (probably from the database)
    int count = getArtistCount(sex, place, errors);
    artists = getArtistData(sex, place, count, errors);
          // Store in the cache
    cacheAdmin.putInCache(key, artists);
      } catch (Exception ex) {
          // We have the current content if we want fail-over.
    artists = (Map) nre.getCacheContent();
          // It is essential that cancelUpdate is called if the
          // cached content is not rebuilt
    cacheAdmin.cancelUpdate(key);
    ex.printStackTrace();
      }
  }
 

2、HTTP Response 缓存
这种方式的缓存用来处理整个页面的内容固定,不会根据参数动态改变:
首先在web.xml中配置CacheFilter:
 <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>86400</param-value>
  </init-param>
  <init-param>
   <param-name>scope</param-name>
   <param-value>application</param-value>
  </init-param>
 </filter>

将所有需要缓存的页面加入filter-mapping:
 <filter-mapping>
  <filter-name>Set Character Encoding</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

注意,只有返回状态为200(HttpServletResponse.SC_OK)的内容才会被缓存

3、JSP Tag 缓存
JSP Tag缓存主要用于缓存JSP页面的局部内容:
  <cache:cache key="especialcategory" cron="* 5 * * *">
  <jsp:include page="/ringcategory.do" flush="true" >
    <jsp:param name="ringType" value="1"/>
  </jsp:include>
  </cache:cache>

4、O/R Data Access 缓存
请阅读参考资料的内容获取详情。

参考资料:
Taking the load off: OSCache helps databases cope: http://www.theserverside.com/articles/article.tss?l=OSCacheHelpsDatabases

你可能感兴趣的:(xml,jsp,Web,cache,Access)