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 getAppScopeCaches(ServletContext ctx)
    {
        Cache cache = ServletCacheAdministrator.getInstance(ctx).getAppScopeCache(ctx);
        AbstractConcurrentReadCache cacheMap = getCacheMap(ctx);
        Map map = new HashMap();
         
        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 map = OsCacheUtil.getAppScopeCaches(request.getServletContext());
    request.setAttribute("map", map);
%>



查看 Oscache缓存数据


    

刷新当前页面         释放全部缓存

No Key Value
${status.count} ${entry.key}

释放缓存

刷新当前页面         释放全部缓存

你可能感兴趣的:(Java)