OSCache缓存框架的简单用法
1.创建工程,引入相应的.jar文件
oscache-2.4.1.jar
commons-logging-api-1.1.jar
log4j-1.2.11.jar
2.将oscache.tld放入WIN-INF目录下
3.oscache.properties放入src目录下
如你需要建立磁盘缓存,须修改oscache.properties 中的cache.path信息 (去掉前面的#注释)。
win类路径类似为c:\\app\\cache
unix类路径类似为/opt/myapp/cache
4.在web.xml文件中添加如下配置如下:
<jsp-config>
<taglib>
<taglib-uri>oscache</taglib-uri>
<taglib-location>/WEB-INF/oscache.tld</taglib-location>
</taglib>
</jsp-config>
<taglib>
<taglib-uri>oscache</taglib-uri>
<taglib-location>/WEB-INF/oscache.tld</taglib-location>
</taglib>
</jsp-config>
5.创建cache1.jsp和cache2.jsp两个页面
cache1.jsp具体内容如下:
<
%@ page language=
"java"
import=
"java.util.*"
pageEncoding=
"UTF-8"
%>
< %@ taglib uri= "/WEB-INF/oscache.tld" prefix= "cache" %>
< html>
< body>
没有缓存的日期: < %= new Date() %>< p>
<!--自动刷新-->
< cache:cache time= "10" >
每10秒刷新缓存一次的日期: < %= new Date() %>
< /cache:cache>
< BR/>
<!--手动刷新-->
< cache:cache key= "testcache" >
手动刷新缓存的日期: < %= new Date() %> < p>
< /cache:cache>
< br/>
< a href= "cache2.jsp" >手动刷新< /a>
< /body>
< /html>
< %@ taglib uri= "/WEB-INF/oscache.tld" prefix= "cache" %>
< html>
< body>
没有缓存的日期: < %= new Date() %>< p>
<!--自动刷新-->
< cache:cache time= "10" >
每10秒刷新缓存一次的日期: < %= new Date() %>
< /cache:cache>
< BR/>
<!--手动刷新-->
< cache:cache key= "testcache" >
手动刷新缓存的日期: < %= new Date() %> < p>
< /cache:cache>
< br/>
< a href= "cache2.jsp" >手动刷新< /a>
< /body>
< /html>
cache2.jsp的具体内容如下:
<
%@ page language=
"java"
import=
"java.util.*"
pageEncoding=
"UTF-8"
%>
< %@ taglib uri= "/WEB-INF/oscache.tld" prefix= "cache" %>
< html>
< body>
缓存已刷新...< p>
< cache:flush key= "testcache" scope= "application" />
< a href= "cache1.jsp" >返回< /a>
< /body>
< /html>
< %@ taglib uri= "/WEB-INF/oscache.tld" prefix= "cache" %>
< html>
< body>
缓存已刷新...< p>
< cache:flush key= "testcache" scope= "application" />
< a href= "cache1.jsp" >返回< /a>
< /body>
< /html>
6.现在开始启动程序,然后刷新页面,看看什么效果。
7.再在上面的基础上,修改web.xml文件完整的如下:
<
?xml version=
"1.0"
encoding=
"UTF-8"
?>
< web-app id= "WebApp_ID" version= "2.4"
xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< display-name>OSCache< /display-name>
< jsp-config>
< taglib>
< taglib-uri>oscache< /taglib-uri>
< taglib-location>/WEB-INF/oscache.tld< /taglib-location>
< /taglib>
< /jsp-config>
<!--- 你可以在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>10</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>
< welcome-file-list> < welcome-file>index.jsp< /welcome-file> < /welcome-file-list>
< /web-app>
< web-app id= "WebApp_ID" version= "2.4"
xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< display-name>OSCache< /display-name>
< jsp-config>
< taglib>
< taglib-uri>oscache< /taglib-uri>
< taglib-location>/WEB-INF/oscache.tld< /taglib-location>
< /taglib>
< /jsp-config>
<!--- 你可以在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>10</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>
< welcome-file-list> < welcome-file>index.jsp< /welcome-file> < /welcome-file-list>
< /web-app>