当前位置:网站首页>OsCache缓存监控刷新工具
OsCache缓存监控刷新工具
2022-07-28 11:11:00 【爱码少年】
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>
<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>
<a href="?action=removeAll">释放全部缓存</a>
</p>
</body>
</html>
边栏推荐
- [general database integrated development environment] Shanghai daoning provides you with Aqua Data Studio downloads, tutorials, and trials
- R language uses LM function to build regression model and regression model for transformed data (for example, it is necessary to build regression model for X and y, but they have no linear relationshi
- What functions does MySQL have? Don't look everywhere. Just look at this.
- What is the process of switching c read / write files from user mode to kernel mode?
- 使用c语言实现双向链表
- Tiktok programmer confession special code tutorial (how to play Tiktok)
- Guys, ask me, this can't be checkpoint, because there is a JDBC task whose task status is finished,
- What is WordPress
- 【补题日记】[2022牛客暑期多校2]H-Take the Elevator
- Are interviews all about memorizing answers?
猜你喜欢

拥抱开源指南

对话庄表伟:开源第一课

Autumn recruit offer harvesters, and take the offers of major manufacturers at will

Introduction to web security RADIUS protocol application

Four advantages of verification code to ensure mailbox security

LabVIEW AI visual Toolkit (non Ni vision) download and installation tutorial

Are interviews all about memorizing answers?

移动端人脸风格化技术的应用

zotero文献管理器及其使用姿势(不定时更新)

Top ten application development trends from 2022 to 2023
随机推荐
R language uses LM function to build regression model and regression model for transformed data (for example, it is necessary to build regression model for X and y, but they have no linear relationshi
Two point, three point, 01 point plan [bullet I]
R language uses LM function to build regression model, uses the augmented function of bloom package to store the model results in dataframe, and uses ggplot2 to visualize the regression residual diagr
Install SSL Certificate in Litespeed web server
PFP会是数字藏品的未来吗?
接口测试的作用
Learning notes tree array
A hundred flowers bloom in data analysis engines. Why invest heavily in Clickhouse?
Design a system that supports millions of users
ASP. Net core 6 framework unveiling example demonstration [29]: building a file server
Shell (II)
ZBrush 2022 software installation package download and installation tutorial
Database advanced learning notes - system package
Leecode8 string conversion integer (ATOI)
Google Earth Engine——使用geetool批量下载单景影像以Landsat 8 反演后的NDSI结果
zotero文献管理器及其使用姿势(不定时更新)
The fifth generation verification code of "cloud centered, insensitive and extremely fast" is coming heavily
从零开始Blazor Server(2)--整合数据库
CVPR2021 行人重识别/Person Re-identification 论文+开源代码汇总
What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?