oscache的使用

下载oscache的相关jar包和相关文件,
oscache.properties,oscache.tld,oscache.jar,log4j.properties等相关文件,copy到WEN-INF/下面,下面开始配置使用oscache

在web.xml中配置oscache.tld以便于在页面方便引用oscache标签.
<jsp-config>
<taglib>
<taglib-uri>oscache</taglib>
<taglib-location>/WEN-INF/oscache.tld</taglib-location>
</taglib>
</jsp-config>

在页面中直接引用
<% taglib uri="oscache" prefix="oscache" %>就可以直接使用oscache标签来进行页面级的缓存,

当前时间是:<%= new Date() %><br>

<oscache:cache key="one" duration="5" scope="session">
( 缓存后的时间是:<%= new Date() %>)//每隔5秒钟刷新一次数据
</oscache:cache>
<pre>
这里你会发现是刚才缓存的数据:
<oscache:cache key="one" duration="5">
</oscache:cache>
</pre>

那么这中间的一段jsp代码将会以key="one"缓存在session中,任何其他页面中使用这个key的cache标签都能共享这段存在缓存中的执行结果
考虑一个需求,一个页面是有许多个不同的jsp文件拼出来的
可能在页首有随机的广告,登录用户的信息,系统的即时信息,固定的目录信息等等
这其中可以考虑将固定的目录信息放入缓存中,而其他动态信息则即时刷新
再进一步考虑 有时候页面之间的信息是关联的,只有当其中一条信息的内容变化了才需要去刷新
对于这种需求就可以考虑在<cache:cache/>标签中配置group属性,将不同的具有关联关系的cache内容
分组,这样oscache会自动的帮你检查该组缓存内容的变化情况,如果有任何一子成员组的内容变化了
则会执行刷新,这样就可以在页面实现数据的动态同步
代码如下:

(来源oscache:groupTest.jsp


<%@ page import="java.util.*" %>
<%@ taglib uri="oscache" prefix="cache" %>

<head>
<title>Test Page</title>
<style type="text/css">
body {font-family: Arial, Verdana, Geneva, Helvetica, sans-serif}
</style>
</head>
<body>

<a href="<%= request.getContextPath() %>/">Back to index</a><p>
<hr>Flushing 'group2'<hr>
<cache:flush group='group2' scope='application'/>
<hr>
<cache:cache key='test1' groups='group1,group2' duration='5s'>
<b>Cache Time</b>: <%= (new Date()).getTime() %><br>
This is some cache content test1 that is in 'group1' and 'group2'. Normally it would refresh if it
was more than 5 seconds old, however the <cache:flush group='group2' scope='application'>
tag causes this entry to be flushed on every page refresh.<br>
</cache:cache>
<hr>



这里有两个cache分组group1和group2,将group2设置为每次都执行刷新,所以test1为key的cache每次刷新页面内容都是重新执行过的

<cache:cache key='test2' groups='group1' duration='5s'>
<b>Cache Time</b>: <%= (new Date()).getTime() %><br>
This is some cache content test2 that is in 'group1' (refreshes if more than 5 seconds old)<br>
</cache:cache>
<hr>



而test2只有当间隔时间超过5秒才会更新内容


<cache:cache key='test3' duration='20s'>
<b>Cache Time</b>: <%= (new Date()).getTime() %><br>
This is some cache content test3 that is in 'group1' and 'group2'. The groups are added using the <cache:addgroup /> tag.<br>
<cache:addgroup group='group1'/>
<cache:addgroup group='group2'/>
</cache:cache>
<hr>
<cache:cache key='test4' duration='20s'>
<b>Cache Time</b>: <%= (new Date()).getTime() %><br>
This is some cache content test4 that is in 'group1' and 'group2'. The groups are added using the <cache:addgroups /> tag.<br>
<cache:addgroups groups='group1,group2'/>
</cache:cache>
<hr>
</body>
</html>



<cache:addgroup group='{you_group}'/>可以将所在的cache加入存在的group中

布署项目成功,打开tomcat的manager的时候进不去.发现tomcat-users.xml文件中没有配置相关信息,在这里配置一下,也算是熟悉一下用户的配置了.

<?xml version="1.0" encoding="utf-8">
<tomcat-users>
<role rolename="manager"/>
<user username="admin" password="admin" roles="manager"/>
</tomcat-users>

配置完成后,重新启动tomcat就可以启动项目.在页面上看到详细的效果,oscache使用起来比较简单快捷,一会准备看下ehcache,不知道也是否这么简单易上手.
成长的乐趣,在于分享!
大龄程序员,一路走来,感慨颇多。闲暇时写写字,希望能给同行人一点帮助。
本文版权归作者growithus和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/growithus/p/11012523.html