网站优化技术——OSCache实现页面缓存

缓存技术有:页面缓存(View,html代码)缺点是不能做到实时更新,优点是比二级缓存性能更高;二级缓存(mode/业务层,domain对象)优点是实时更新
缓存产品有:EHCahce、 OSCache、JbossCache(分布式缓存)

页面缓存
1、清除缓存:
<!-- refresh为true将会导致缓存的内容过期而被清除,简单地说,该属性为true用于清除缓存 -->
  <!-- <oscache:flush scope="application"/>清除application范围内的所有缓存;
  <oscache:flush scope="session" key="huhui"/>清除session范围内的key为huhui的缓存
  <oscache:flush scope="application" group="hu"/>清除application范围内组名为hu内的所有缓存 -->
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.opensymphony.com/oscache" prefix="oscache" %>
<oscache:flush scope="applocation"/>缓存已清除

2、局部缓存:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="oscache" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
  </head>
 
  <body><!-- 这是局部缓存 -->
  <oscache:cache key="huhui" scope="session" time="15" refresh="${param.refresh }">
  <!-- 是使用Map对象来存储缓存的,默认的key是uri路径,如:/oscache/index.jsp,也可以指定它的key -->
  <!-- 缓存默认存放在application范围,缓存时间默认为3600秒,即1小时 -->
    <div><%=new Date() %></div>
  </oscache:cache>
  当前时间:<%=new Date() %>
  </body>
</html>

3、全局缓存:web.xml
<!-- 设置页面的全局缓存 -->
  <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>7200</param-value>
</init-param>
<init-param>
    <param-name>scope</param-name>
    <param-value>application</param-value>
</init-param>
   </filter>
   <filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>/product/list.do</url-pattern>
   </filter-mapping>

4、内存缓存/硬盘缓存(推荐使用内存缓存,比硬盘缓存要快得多)oscache.properties
#指定是否使用内存缓存,默认值为true,即使用内存缓存
cache.memory=true
#指定缓存的容量,默认的容量是无限的
cache.capacity=30000

#如果要使用硬盘缓存,可以这样设置:
cache.memory=false
#指定缓存保存的路径
cache.path=E:\\oscache
#用于设置持久化的类cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener

猜你喜欢

转载自javahuhui.iteye.com/blog/1481686