OSCache-JSP页面缓存(2)
如果在jsp中使用如下标签
<cache:cache key="foobar" scope="session"> some jsp content </cache:cache>
那么这中间的一段jsp代码将会以key="foobar"缓存在session中,任何其他页面中使用这个key的cache标签都能共享这段存在缓存中的执行结果。
考虑一个需求,一个页面是有许多个不同的jsp文件拼出来的,可能在页首有随机的广告,登录用户的信息,系统的即时信息,固定的目录信息等等;这其中可以考虑将固定的目录信息放入缓存中,而其他动态信息则即时刷新;再进一步考虑有时候页面之间的信息是关联的,只有当其中一条信息的内容变化了才需要去刷新。
对于这种需求就可以考虑在<cache:cache/>标签中配置group属性,将不同的具有关联关系的cache内容分组,这样oscache会自动的帮你检查该组缓存内容的变化情况,如果有任何一子成员组的内容变化了则会执行刷新,这样就可以在页面实现数据的动态同步。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="java.text.SimpleDateFormat"%> <%@ 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分组group1和group2,将group2设置为每次都执行刷新,所以test1为key的cache每次刷新页面内容都是重新执行过的 --> <cache:cache key='test1' groups='group1,group2' duration='5s'> <b>Cache Time1</b>: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())%><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> <!-- test2只有当间隔时间超过5秒才会更新内容 --> <cache:cache key='test2' groups='group1' duration='5s'> <b>Cache Time2</b>: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())%><br> This is some cache content test2 that is in 'group1' (refreshes if more than 5 seconds old)<br> </cache:cache> <hr>
<!--每隔20秒刷新一次-> <cache:cache key='test3' duration='20s'> <b>Cache Time3</b>: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())%><br> This is some cache content test3 that is in 'group1' and 'group2'. The groups are added using the tag.<br> </cache:cache> <hr>
<!--实时刷新--> <cache:cache key='test4' duration='20s'> <b>Cache Time4</b>: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())%><br> This is some cache content test4 that is in 'group1' and 'group2'. The groups are added using the tag.<br> <cache:addgroups groups='group1,group2' /> </cache:cache> <hr> </body> </html>
<cache:addgroup group='{you_group}'/>可以将所在的you_group加入当前所在位置的group中
分类:
OSCache
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)