---
title: "在jsp页面中使用oscache标签实现可配置的页面缓存"
author: "itsnack"
date: "2010-11-25T01:26:36Z"
tags: []
categories: ["编程语言"]
slug: "jsp-oscache"
draft: false
lastmod: "2010-11-25T01:26:36Z"
id: 823092
url: "https://itsnack.iteye.com/blog/823092"
---

### [在jsp页面中使用oscache标签实现可配置的页面缓存](/blog/163209)

**文章分类:[Java编程](http://www.iteye.com/blogs/category/java)**

最近在学习oscache的相关内容，写点东西作为巩固

如果在jsp中使用如下标签

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. &lt;cache:cache key="foobar" scope="session"&gt;
2. ... some jsp content ...
3. &lt;/cache:cache&gt;

```
<cache:cache key="foobar" scope="session">
         ... some jsp content ...
</cache:cache>
```

 那么这中间的一段jsp代码将会以key="foobar"缓存在session中，任何其他页面中使用这个key  
的cache标签都能共享这段存在缓存中的执行结果

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

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. &lt;%@ page **import**="java.util.\*" %&gt;
02. &lt;%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %&gt;
03. &lt;head&gt;
04. &lt;title&gt;Test Page&lt;/title&gt;
05. &lt;style type="text/css"&gt;
06. body {font-family: Arial, Verdana, Geneva, Helvetica, sans-serif}
07. &lt;/style&gt;
08. &lt;/head&gt;
09. &lt;body&gt;
10. &lt;a href="&lt;%= request.getContextPath() %&gt;/"&gt;Back to index&lt;/a&gt;&lt;p&gt;
11. &lt;hr&gt;Flushing 'group2'...&lt;hr&gt;
12. &lt;cache:flush group='group2' scope='application'/&gt;
13. &lt;hr&gt;
14. &lt;cache:cache key='test1' groups='group1,group2' duration='5s'&gt;
15. &lt;b&gt;Cache Time&lt;/b&gt;: &lt;%= (**new** Date()).getTime() %&gt;&lt;br&gt;
16. This is some cache content test1 that is in 'group1' and 'group2'. Normally it would refresh **if** it
17. was more than 5 seconds old, however the &lt;cache:flush group='group2' scope='application'&gt;
18. tag causes **this** entry to be flushed on every page refresh.&lt;br&gt;
19. &lt;/cache:cache&gt;
20. &lt;hr&gt;

```
<%@ page import="java.util.*" %>
<%@ taglib uri="http://www.opensymphony.com/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每次刷新页面内容都是重新执行过的

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. &lt;cache:cache key='test2' groups='group1' duration='5s'&gt;
2. &lt;b&gt;Cache Time&lt;/b&gt;: &lt;%= (**new** Date()).getTime() %&gt;&lt;br&gt;
3. This is some cache content test2 that is in 'group1' (refreshes **if** more than 5 seconds old)&lt;br&gt;
4. &lt;/cache:cache&gt;
5. &lt;hr&gt;

```
<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秒才会更新内容

 

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. &lt;cache:cache key='test3' duration='20s'&gt;
02. &lt;b&gt;Cache Time&lt;/b&gt;: &lt;%= (**new** Date()).getTime() %&gt;&lt;br&gt;
03. This is some cache content test3 that is in 'group1' and 'group2'. The groups are added using the &lt;cache:addgroup /&gt; tag.&lt;br&gt;
04. &lt;cache:addgroup group='group1'/&gt;
05. &lt;cache:addgroup group='group2'/&gt;
06. &lt;/cache:cache&gt;
07. &lt;hr&gt;
08. &lt;cache:cache key='test4' duration='20s'&gt;
09. &lt;b&gt;Cache Time&lt;/b&gt;: &lt;%= (**new** Date()).getTime() %&gt;&lt;br&gt;
10. This is some cache content test4 that is in 'group1' and 'group2'. The groups are added using the &lt;cache:addgroups /&gt; tag.&lt;br&gt;
11. &lt;cache:addgroups groups='group1,group2'/&gt;
12. &lt;/cache:cache&gt;
13. &lt;hr&gt;
14. &lt;/body&gt;
15. &lt;/html&gt;

```
<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>
```

 

  &lt;cache:addgroup group='{you\_group}'/&gt;可以将所在的cache加入存在的group中