当前位置:网站首页>OSCache cache monitoring Refresh Tool
OSCache cache monitoring Refresh Tool
2022-07-28 11:50:00 【Love code youth】
OSCache It's a set Java Write a cache framework ( Or the solution ), It is mainly used for page caching ,Servlet cache , Or any other object , And support cluster .
But I didn't OsCache Monitoring tools , So we can only use reflection mechanism to crack it violently !
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 Caching tool class
*
* @author 00fly
* @version [ Version number , 2016-4-7]
* @see [ Related classes / Method ]
* @since [ product / Module version ]
*/
public class OsCacheUtil
{
static Logger log = LoggerFactory.getLogger(OsCacheUtil.class);
/**
* Obtained by reflection mechanism Cache Private member variables cacheMap
*
* @return
*/
public static AbstractConcurrentReadCache getCacheMap(ServletContext ctx)
{
// obtain Cache Object instances
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;
}
/**
* obtain ServletCache All Application Scope Of 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;
}
/**
* Release cached content
*
* @param ctx
* @param key
* @see [ class 、 class # Method 、 class # member ]
*/
public static void removeEntry(ServletContext ctx, String key)
{
Cache cache = ServletCacheAdministrator.getInstance(ctx).getAppScopeCache(ctx);
log.info("------ Release cache : key={} ------", key);
cache.removeEntry(key);
}
/**
* Release all cached contents
*
* @param ctx
* @param key
* @see [ class 、 class # Method 、 class # member ]
*/
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("------ Release cache : 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> see Oscache Cache data </title>
</head>
<body align="center" style="font-size:15px">
<p>
<a href="#" onclick="location.reload();"> Refresh current page </a>
<a href="?action=removeAll"> Free all cache </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}"> Release cache </a></td>
<td><c:out value="${entry.value}" escapeXml="true" /></td>
</tr>
</c:forEach>
</table>
<p>
<a href="#" onclick="location.reload();"> Refresh current page </a>
<a href="?action=removeAll"> Free all cache </a>
</p>
</body>
</html>
边栏推荐
- The fifth generation verification code of "cloud centered, insensitive and extremely fast" is coming heavily
- Let me think about Linear Algebra: a summary of basic learning of linear algebra
- [极客大挑战 2019]BabySQL-1|SQL注入
- A new mode of one-stop fixed asset management
- Article summary of MinGW installation and use
- 中国业务型CDP白皮书 | 爱分析报告
- 强缓存、协商缓存具体过程
- A lock faster than read-write lock. Don't get to know it quickly
- Function of interface test
- Why does acid food hurt teeth + early periodontitis
猜你喜欢

CVPR2020 best paper:对称可变形三维物体的无监督学习

Deployment and use of Minio distributed object storage

Today's sleep quality record 74 points

强缓存、协商缓存具体过程

What is the process of switching c read / write files from user mode to kernel mode?

简单选择排序与堆排序

中国业务型CDP白皮书 | 爱分析报告

Update dev (development version) of the latest win11

Cvpr2021 pedestrian re identification /person re identification paper + summary of open source code

WPF layout controls are scaled up and down with the window, which is suitable for multi-resolution full screen filling applications
随机推荐
大佬们,问下,这个不能checkpoint,因为有个jdbc的task任务状态是FINISHED,
echo -ne(echo line)
[极客大挑战 2019]BabySQL-1|SQL注入
在生产环境中每天Oracle监控到的无效对象一般怎么去处理?
一种比读写锁更快的锁,还不赶紧认识一下
301. Delete invalid brackets
Sirius network verification source code / official genuine / included building tutorial
jar 包内文件的遍历以及文件的拷贝
Article summary of MinGW installation and use
Good use explosion! The idea version of postman has been released, and its functions are really powerful
Digital twin rail transit: "intelligent" monitoring to clear the pain points of urban operation
Localization, low latency, green and low carbon: Alibaba cloud officially launched Fuzhou data center
直接插入排序与希尔排序
Function of interface test
哪位大神帮看下 oracle number类型解析 怎么搞 Struct{scale=15,val
I want to ask you guys, if there is a master-slave switch when CDC collects mysql, is there any solution
业务可视化-让你的流程图'Run'起来(4.实际业务场景测试)
Excel shortcut keys (letters + numbers) Encyclopedia
R语言-用于非平衡数据集的一些度量指标
AlexNet—论文分析及复现