OsCache缓存监控刷新工具

   OSCache是一套用Java编写的缓存框架(或者说解决方案),它主要用于页面缓存,Servlet缓存,或者其它任意的对象,且支持集群。
   但是居然没有OsCache的监控工具,所以只能用反射机制暴力破解了!

OsCacheUtil.java

package com.fly.core;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.ServletContext;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
 
/**
 * OsCache缓存工具类
 * 
 * @author 00fly
 * @version [版本号, 2016-4-7]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class OsCacheUtil
{
     
    static Logger log = LoggerFactory.getLogger(OsCacheUtil.class);
     
    /**
     * 通过反射机制获取Cache私有成员变量cacheMap
     * 
     * @return
     */
    public static AbstractConcurrentReadCache getCacheMap(ServletContext ctx)
    {
        // 获取Cache对象实例
        Cache cache = ServletCacheAdministrator.getInstance(ctx).getAppScopeCache(ctx);
        AbstractConcurrentReadCache cacheMap = null;
        try
        {
            Field field = Cache.class.getDeclaredField("cacheMap");
            field.setAccessible(true);
            cacheMap = (AbstractConcurrentReadCache)field.get(cache);
            field.setAccessible(false);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            log.warn("can't get oscache Cache.cacheMap!", e);
        }
        return cacheMap;
    }
     
    /**
     * 获取ServletCache的全部Application Scope的cache
     * 
     * @return
     * @throws NeedsRefreshException
     */
    public static Map<String, Object> getAppScopeCaches(ServletContext ctx)
    {
        Cache cache = ServletCacheAdministrator.getInstance(ctx).getAppScopeCache(ctx);
        AbstractConcurrentReadCache cacheMap = getCacheMap(ctx);
        Map<String, Object> map = new HashMap<String, Object>();
         
        for (Object key : cacheMap.keySet())
        {
            String keyStr = (String)key;
             
            Object value;
            try
            {
                value = cache.getFromCache(keyStr);
                map.put(keyStr, value);
            }
            catch (NeedsRefreshException e)
            {
                e.printStackTrace();
                log.error("failed get cacheMap data: key={}", keyStr);
                cache.cancelUpdate(keyStr);
            }
        }
        return map;
    }
     
    /**
     * 释放缓存内容
     * 
     * @param ctx
     * @param key
     * @see [类、类#方法、类#成员]
     */
    public static void removeEntry(ServletContext ctx, String key)
    {
        Cache cache = ServletCacheAdministrator.getInstance(ctx).getAppScopeCache(ctx);
        log.info("------释放缓存: key={} ------", key);
        cache.removeEntry(key);
    }
     
    /**
     * 释放全部缓存内容
     * 
     * @param ctx
     * @param key
     * @see [类、类#方法、类#成员]
     */
    public static void removeAll(ServletContext ctx)
    {
        Cache cache = ServletCacheAdministrator.getInstance(ctx).getAppScopeCache(ctx);
        AbstractConcurrentReadCache cacheMap = getCacheMap(ctx);
        if (cacheMap.isEmpty())
        {
            return;
        }
        for (Object key : cacheMap.keySet())
        {
            String keyStr = (String)key;
            log.info("------释放缓存: key={} ------", keyStr);
            cache.removeEntry(keyStr);
        }
    }
}

oscache.jsp

<%@page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="java.util.Map,com.fly.core.OsCacheUtil"%>
<%
    String action = request.getParameter("action");
    String key = request.getParameter("key");
    if ("remove".equals(action) && key != null && key.length() > 0)
    {
        OsCacheUtil.removeEntry(request.getServletContext(), key);
    }
    else if ("removeAll".equals(action))
    {
        OsCacheUtil.removeAll(request.getServletContext());
    }
    Map<String, Object> map = OsCacheUtil.getAppScopeCaches(request.getServletContext());
    request.setAttribute("map", map);
%>
<html>
<head>
<meta charset="utf-8">
<title>查看 Oscache缓存数据</title>
</head>
<body align="center" style="font-size:15px">
    <p>
        <a href="#" onclick="location.reload();">刷新当前页面</a> &nbsp; &nbsp;
        &nbsp; &nbsp; <a href="?action=removeAll">释放全部缓存</a>
    </p>
    <table border="1" cellpadding="10" cellspacing="1"
        style="font-size:14px">
        <tr>
            <th width="2%">No</th>
            <th width="5%">Key</th>
            <th>Value</th>
        </tr>
        <c:forEach var="entry" items="${map}" varStatus="status">
            <tr width="95%">
                <td>${status.count}</td>
                <td>${entry.key} <br> <br> <a
                    href="?action=remove&key=${entry.key}">释放缓存</a></td>
                <td><c:out value="${entry.value}" escapeXml="true" /></td>
            </tr>
        </c:forEach>
    </table>
    <p>
        <a href="#" onclick="location.reload();">刷新当前页面</a> &nbsp; &nbsp;
        &nbsp; &nbsp; <a href="?action=removeAll">释放全部缓存</a>
    </p>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_16127313/article/details/84196206