Java web开发——使用Oscache来实现页面缓存

        如果想使用Oscache来实现页面缓存,只要导入相应jar包,然后如果想缓存jsp页面内容,需要引入:

 <%@ taglib uri="http://www.opensymphony.com/oscache" prefix="oscache" %>

假如需要缓存当前时间:

现在时间:<%=new Date() %><br>
<oscache:cache time="4">
缓存时间:<%=new Date() %><br>
</oscache:cache>

那么当刷新该页面时第一个时间一直改变,第二个时间每隔4秒才会改变。假如上述页面为默认页面(index.jsp)那么在访问地址

http://localhost:8080/Oscache/index.jsp 

http://localhost:8080/Oscache/

时缓存的时间将会变化,上面2个地址都访问同一个页面,为什么缓存会变化?因为在缓存数据结构中以map,key存储浏览器访问url,上面2个url不一致,缓存肯定变化。此外key也可以自己指定:

现在时间:<%=new Date() %><br>
<oscache:cache time="4" key="mykey">
缓存时间:<%=new Date() %><br>
</oscache:cache>

上面的缓存默认存储在application域当中。当然也可以自己更改,改变缓存到Session:

现在时间:<%=new Date() %><br>
<oscache:cache time="4" scope="session">
缓存时间:<%=new Date() %><br>
</oscache:cache>

缓存也可以存放在文件中,以实现持久化,首先创建oscache.properties(这个配置文件必须在classpath下面):

cache.memory=false
cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
cache.path=F:\\cache

如果想让Oscache整合web项目(即缓存页面),如,商品页面访问量特别大,给商品页面缓存(Items路径下所有请求都缓存)。可以在web.xml中进行如下配置:

  <filter>
  <filter-name>oscache</filter-name>
  <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
  <init-param>
  <param-name>time</param-name>
  <param-value>3600</param-value>
  </init-param>
  <init-param>
  <param-name>scope</param-name>
  <param-value>application</param-value>
  </init-param>
  </filter>

  <filter-mapping>
  <filter-name>oscache</filter-name>
  <url-pattern>/items/*</url-pattern>
  </filter-mapping>

测试缓存:打一个断点(给商品查询列表),第一次断点必须走,第二次断点不走,走缓存页面。

本工程用于研究如何借助Ehcache缓存框架实现页面缓存 本工程编码方式:UTF-8 本工程开发工具:MyEclipse 说明: 1、ehcache.xml和ehcache.xsd两个文件可以在下在下载下来的名为“ehcache-core-x.x.x-distribution.tar.gz”压缩文件中找到 2、由于要实现Ehcache缓存页面,所以必须要添加“ehcache-web-2.0.4.jar” jar包,该jar包主要用于辅助Ehcache实现页面缓存 注意: 本web工程的发布不要使用Tomcat7,否则会出现如下异常: 2015-3-25 9:53:50 org.apache.catalina.loader.WebappClassLoader loadClass 信息: Illegal access: this web application instance has been stopped already. Could not load net.sf.ehcache.store.disk.DiskStore$KeySet. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. java.lang.IllegalStateException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at net.sf.ehcache.store.disk.DiskStore.keySet(DiskStore.java:560) at net.sf.ehcache.store.disk.DiskStorageFactory$DiskExpiryTask.run(DiskStorageFactory.java:838) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:181) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:205) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) 相关jar包下载地址: Ehcache 对象、数据缓存:http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盡盡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值