最近一个项目用到OsCache的页面片段缓存,google了一下居然没有找到OsCache的监控工具(list all keys from cache),于是大略读了一下OSCache-2.4.1的源码,发现Cache.java类的cacheMap定义成了私有变量,如下:
/**
* The actual cache map. This is where the cached objects are held.
*/
private AbstractConcurrentReadCache cacheMap = null;
所以只能用反射机制暴力破解了(按照Sun公司的JVM规范是许可的^_^),主要代码如下:
1) OsCacheUtil.java/**
* 通过反射机制获取Cache私有成员变量cacheMap, 2012/10/8, by jeffsang
* @return
*/
public static AbstractConcurrentReadCache getCacheMap(ServletContext ctx) {
//获取Cache对象实例
Cache cache = ServletCacheAdministrator.getInstance(ctx).getAppScopeCache(ctx);
//通过反射机制获取Cache私有成员变量cacheMap
AbstractConcurrentReadCache cacheMap = null;
try {
Field field = Cache.class.getDeclaredField("cacheMap" );
field.setAccessible( true);
cacheMap = (AbstractConcurrentReadCache) field.get(cache);
field.setAccessible( false);
}
catch (Exception e) {
log.warn( "can't acquire oscache Cache.cacheMap! " , e);
}
return cacheMap;
}
/**
* 获取ServletCache的全部Application Scope的cache, 2012/10/8, by jeffsang
* @return
*/
public static Map getAppScopeCaches(ServletContext ctx) {
Map map = new HashMap();
//获取Cache对象实例
Cache cache = ServletCacheAdministrator.getInstance(ctx).getAppScopeCache(ctx);
//通过反射机制获取Cache私有成员变量cacheMap
AbstractConcurrentReadCache cacheMap = getCacheMap(ctx);
//返回包含在cacheMap中的Map关系的 Set视图。
@SuppressWarnings("unchecked")
Set> setEntry = cacheMap.entrySet();
//使用Iterator遍历器
//Iterator> it = cacheMap.entrySet().iterator();
//使用for遍历cacheMap中的entrySet
for (Map.Entry entry : setEntry) {
String key = entry.getKey();
Object value = cache.getFromCache(key);
//以下方法只能得到CacheEntry对象实例Id
//Object value = entry.getValue();
map.put(key, value);
}
return map;
}
2) oscache_list.jsp<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.*" %>
<%@ page import="com.lasun.util.OsCacheUtil" %>
<%
Map<String, Object> map = OsCacheUtil.getAppScopeCaches(request.getServletContext());
request.setAttribute("map", map);
%>
<table border="1">
<tr>
<th>No</th>
<th>Key</th>
<th>Value</th>
</tr>
<c:forEach var="entry" items="${map}" varStatus="status">
<tr>
<td>${status.index+1}</td>
<td>${entry.key}</td>
<td>${entry.value}</td>
</tr>
</c:forEach>
</table>
小结:本例子只实现ServletCache中的Application Scop的cache list功能;通过Listener或持久化也能实现监控。
分享到:
相关推荐
oscache缓存技术,压缩包中有详细代码及步骤
这里结合 天气预报的webservice 展示了OsCache框架的具体使用方法 项目可直接运行 ,代码简洁清晰
oscache缓存技术入门实例
Hibernate OSCache缓存 Hibernate OSCache缓存
oscache缓存使用总结
OSCache缓存框架的简单用法,希望对大家有所帮助!!!
一个OSCache缓存技术的关键zip包
OSCache标记库由OpenSymphony设计,它是一种开创性的缓存方案,它提供了在现有JSP页面之内实现内存缓存的功能。OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存...
描述了oscahce在JAVA开发中的应用和配置说明
1、OSCache是什么? 2、OSCache的特点 3、有关“用OSCache进行缓存对象”的研究
OSCache标记库由OpenSymphony设计,它是一种开创性的缓存方案,它提供了在现有JSP页面之内实现内存缓存的功能。OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存...
oscache,java,缓存机制的使用
NULL 博文链接:https://davidxiaozhi.iteye.com/blog/1045223
NULL 博文链接:https://baobeituping.iteye.com/blog/748346
二级缓存插件OScache配置,很好的学习资料
oscache-java缓存框架插件和安装教程,使用教程一步到位
NULL 博文链接:https://hihitiger.iteye.com/blog/966649
NULL 博文链接:https://yanxiansheng.iteye.com/blog/1636690
博文链接:https://zem.iteye.com/blog/102828