当前位置:网站首页>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>
边栏推荐
- Design a system that supports millions of users
- Know the optical fiber interface and supporting optical fiber cable of can optical fiber converter in fire alarm networking
- Understand how to prevent tampering and hijacking of device fingerprints
- Update dev (development version) of the latest win11
- go status. Go status code definition
- [applet] how to notify users of wechat applet version update?
- 一些多参数函数的具体作用
- OsCache缓存监控刷新工具
- R language uses LM function to build regression model with interactive items, and uses: sign (colon) to represent the interaction of variables (colon is pure multiplication, excluding the constituent
- Six papers that must be seen in the field of target detection
猜你喜欢

Random talk on GIS data (V) - geographic coordinate system

Ripro9.0 revised and upgraded version +wp two beautification packages + rare plug-ins

The fifth generation verification code of "cloud centered, insensitive and extremely fast" is coming heavily

从0开发一个自己的npm包
![[pyGame practice] the super interesting bubble game is coming - may you be childlike and always happy and simple~](/img/3b/c06c140cd107b1158056e41b954e2e.png)
[pyGame practice] the super interesting bubble game is coming - may you be childlike and always happy and simple~

Top ten application development trends from 2022 to 2023
![[geek challenge 2019] babysql-1 | SQL injection](/img/21/b5b4727178a585e610d743e92248f7.png)
[geek challenge 2019] babysql-1 | SQL injection

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

Unity鼠标带动物体运动的三种方法

拥抱开源指南
随机推荐
MySQL (version 8.0.16) command and description
A lock faster than read-write lock. Don't get to know it quickly
Today's sleep quality record 74 points
Embrace open source guidelines
Go deadlock - when the channel meets mutex
[collection] Advanced Mathematics: Capriccio of stars
Object to object mapping -automapper
R language - some metrics for unbalanced data sets
Quickly deploy mqtt clusters on AWS using terraform
Static proxy instance
jar 包内文件的遍历以及文件的拷贝
保障邮箱安全,验证码四个优势
The fifth generation verification code of "cloud centered, insensitive and extremely fast" is coming heavily
echo -ne(echo line)
R language uses oneway The test function performs one-way ANOVA
Zotero document manager and its use posture (updated from time to time)
Blackboard cleaning effect shows H5 source code + very romantic / BGM attached
Sirius network verification source code / official genuine / included building tutorial
从零开始Blazor Server(2)--整合数据库
从0开发一个自己的npm包