当前位置:网站首页>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>
边栏推荐
- Summary of common RSA related problems in CTF: basic RSA encryption and decryption
- Flash point list of cross platform efficiency software
- Database advanced learning notes - storage structure
- 301. Delete invalid brackets
- R语言-用于非平衡数据集的一些度量指标
- Anonymous implementation class object of interface
- 一文看懂设备指纹如何防篡改、防劫持
- Shell (II)
- MySQL离线同步到odps的时候 可以配置动态分区吗
- Left connection and right connection of MySQL (the difference between inner connection and natural connection)
猜你喜欢

Design a system that supports millions of users
![[applet] how to notify users of wechat applet version update?](/img/04/848a3d2932e0dc73adb6683c4dca7a.png)
[applet] how to notify users of wechat applet version update?

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

可视化大型时间序列的技巧。

Digital twin rail transit: "intelligent" monitoring to clear the pain points of urban operation

保障邮箱安全,验证码四个优势

Outlook suddenly becomes very slow and too laggy. How to solve it

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

Design and implementation of SSM personal blog system

Understand how to prevent tampering and hijacking of device fingerprints
随机推荐
Final modifier attribute
【补题日记】[2022牛客暑期多校2]L-Link with Level Editor 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
Four advantages of verification code to ensure mailbox security
14、用户web层服务(二)
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
Excel shortcut keys (letters + numbers) Encyclopedia
An example of the mandatory measures of Microsoft edge browser tracking prevention
Outlook suddenly becomes very slow and too laggy. How to solve it
Anonymous implementation class object of interface
async await如何实现并发
Blackboard cleaning effect shows H5 source code + very romantic / BGM attached
How to use JWT for authentication and authorization
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
Database advanced learning notes -- object type
Boutique scheme | Haitai Fangyuan full stack data security management scheme sets a "security lock" for data
Function of interface test
从0开发一个自己的npm包
MySQL离线同步到odps的时候 可以配置动态分区吗
Network communication protocol classification and IP address