Skip to content

Commit

Permalink
1:往redis set 数据方法变更,使用setEx方法代替 set + expire;
Browse files Browse the repository at this point in the history
2:优化AdminServlet
  • Loading branch information
jiayu.qiu committed Mar 11, 2015
1 parent f001f0b commit b4047e8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 24 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,26 @@ AutoLoadHandler中需要缓存通过**深度复制**后的参数。
5. 通过Spring配置,能很简单方便使用,也很容易修改维护;支持配置多种缓存实现;
6. 可以通过继承AbstractCacheManager,自己实现维护的操作方法,也可以增加除Memcache、Redis外的缓存技术支持。

##缓存管理页面

从1.0版本开始增加缓存管理页面。

web.xml配置:

<servlet>
<servlet-name>cacheadmin</servlet-name>
<servlet-class>com.jarvis.cache.admin.servlet.AdminServlet</servlet-class>
<init-param>
<param-name>cacheManagerNames</param-name>
<param-value>cachePointCut</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cacheadmin</servlet-name>
<url-pattern>/cacheadmin</url-pattern>
</servlet-mapping>

显示内容,如下图所示:
![Alt 缓存管理](/doc/cache_admin.png "缓存管理")

Binary file added doc/cache_admin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/main/java/com/jarvis/cache/AbstractCacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public AbstractCacheManager(AutoLoadConfig config) {
public AutoLoadHandler<T> getAutoLoadHandler() {
return this.autoLoadHandler;
}

/**
* 生成缓存 Key
* @param pjp
Expand Down Expand Up @@ -177,6 +177,7 @@ private T loadData(ProceedingJoinPoint pjp, AutoLoadTO autoLoadTO, String cacheK
}
}

@Override
public void destroy() {
autoLoadHandler.shutdown();
autoLoadHandler=null;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/jarvis/cache/ICacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface ICacheManager<T> {
void delete(String key);

AutoLoadHandler<T> getAutoLoadHandler();

void destroy();
}
76 changes: 57 additions & 19 deletions src/main/java/com/jarvis/cache/admin/servlet/AdminServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.jarvis.cache.ICacheManager;
import com.jarvis.cache.to.AutoLoadTO;
import com.jarvis.lib.util.BeanUtil;

/**
* 缓存管理页面
Expand All @@ -38,24 +39,34 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html");
String cacheManagerName=req.getParameter("cacheManagerName");
if(null == cacheManagerNames || cacheManagerNames.length == 0) {
String errMsg="set \"cacheManagerNames\" parameter with bean names of com.jarvis.cache.ICacheManager instance";
resp.getWriter().println(errMsg);
return;
}
if(null == cacheManagerName || cacheManagerName.trim().length() == 0) {
cacheManagerName=cacheManagerNames[0];
}
printHtmlHead(resp, cacheManagerName);
try {
if(null == cacheManagerNames || cacheManagerNames.length == 0) {
String errMsg="set \"cacheManagerNames\" parameter with bean names of com.jarvis.cache.ICacheManager instance";
throw new Exception(errMsg);
}

ApplicationContext ctx=
WebApplicationContextUtils.getRequiredWebApplicationContext(req.getSession().getServletContext());
ICacheManager<?> cacheManager=(ICacheManager<?>)ctx.getBean(cacheManagerName);
if(null == cacheManager) {
String errMsg=cacheManagerName + " is not exists!";
throw new Exception(errMsg);
}
doServices(req, resp, cacheManager);
printForm(resp, cacheManagerName);
printList(resp, cacheManager);
String act=req.getParameter("act");
if(null != act) {
doServices(req, resp, cacheManager);
} else {
printForm(resp, cacheManagerName);
printList(req, resp, cacheManager, cacheManagerName);
}
} catch(Exception e) {
resp.getOutputStream().println(e.getMessage());
e.printStackTrace();
resp.getWriter().println(e.getMessage());
}

printCloseHtml(resp);
Expand All @@ -66,10 +77,24 @@ private void doServices(HttpServletRequest req, HttpServletResponse resp, ICache
String cacheKey=req.getParameter("cacheKey");
if("removeCache".equals(act)) {
cacheManager.delete(cacheKey);
resp.getWriter().println("处理成功!");
} else if("removeAutoloadTO".equals(act)) {
cacheManager.getAutoLoadHandler().removeAutoLoadTO(cacheKey);
resp.getWriter().println("处理成功!");
} else if("resetLastLoadTime".equals(act)) {
cacheManager.getAutoLoadHandler().resetAutoLoadLastLoadTime(cacheKey);
resp.getWriter().println("处理成功!");
} else if("showArgs".equals(act)) {
AutoLoadTO tmpTO=cacheManager.getAutoLoadHandler().getAutoLoadTO(cacheKey);
if(null != tmpTO && null != tmpTO.getArgs() && tmpTO.getArgs().length > 0) {
Object[] args=tmpTO.getArgs();
int len=args.length;
StringBuilder html=new StringBuilder();
for(int i=0; i < len; i++) {
html.append("#args[" + i + "] = ").append(BeanUtil.toString(args[i])).append("<hr/>");
}
resp.getWriter().println(html.toString());
}
}
}

Expand Down Expand Up @@ -113,12 +138,12 @@ private void printHtmlHead(HttpServletResponse resp, String cacheManagerName) th
html.append(" document.getElementById(\"updateCacheForm\").submit();");
html.append("}}");
html.append("</script></head><body>");
resp.getOutputStream().println(html.toString());
resp.getWriter().println(html.toString());
}

private void printForm(HttpServletResponse resp, String cacheManagerName) throws IOException {
StringBuilder html=new StringBuilder();
html.append("<form action=\"\">");
html.append("<form action=\"\" method=\"get\">");
html.append("cache manager bean name:");
html.append("<select name=\"cacheManagerName\">");
for(String tmpName: cacheManagerNames) {
Expand All @@ -129,15 +154,21 @@ private void printForm(HttpServletResponse resp, String cacheManagerName) throws
html.append("<input type=\"submit\" value=\"更改缓存\"></input>");
html.append("</form>");
html.append("cache key:<input type=\"text\" id=\"deleteCacheKey\"/> <input type=\"button\" onclick=\"removeCache(document.getElementById('deleteCacheKey').value)\" value=\"删除缓存\"/>");
html.append("<form id=\"updateCacheForm\" action=\"\">");
html.append("<form id=\"updateCacheForm\" action=\"\" method=\"get\" target=\"_blank\">");
html.append("<input type=\"hidden\" id=\"act\" name=\"act\" value=\"\" />");
html.append("<input type=\"hidden\" id=\"cacheKey\" name=\"cacheKey\" value=\"\" />");
html.append("<input type=\"hidden\" id=\"cacheManagerName\" name=\"cacheManagerName\" value=\"\" />");
html.append("</form>");
resp.getOutputStream().println(html.toString());
resp.getWriter().println(html.toString());
}

private void printList(HttpServletResponse resp, ICacheManager<?> cacheManager) throws IOException {
private void printList(HttpServletRequest req, HttpServletResponse resp, ICacheManager<?> cacheManager, String cacheManagerName)
throws IOException {
AutoLoadTO queue[]=cacheManager.getAutoLoadHandler().getAutoLoadQueue();
if(null == queue || queue.length == 0) {
resp.getWriter().println("自动加载队列中无数据!");
return;
}
StringBuilder html=new StringBuilder();
html.append("<table cellpadding=\"0\" cellspacing=\"0\">");
html.append(" <tr>");
Expand All @@ -154,8 +185,9 @@ private void printList(HttpServletResponse resp, ICacheManager<?> cacheManager)
html.append(" <th>remove cache </th>");
html.append(" <th>remove AutoloadTO </th>");
html.append(" <th>reset last load time </th>");
html.append(" <th>show arguments </th>");
html.append(" </tr>");
AutoLoadTO queue[]=cacheManager.getAutoLoadHandler().getAutoLoadQueue();

for(AutoLoadTO tmpTO: queue) {
ProceedingJoinPoint pjp=tmpTO.getJoinPoint();
String className=pjp.getTarget().getClass().getName();
Expand All @@ -166,9 +198,9 @@ private void printList(HttpServletResponse resp, ICacheManager<?> cacheManager)
html.append(" <td>" + getDateFormat(tmpTO.getLastRequestTime()) + "</td>");
html.append(" <td>" + getDateFormat(tmpTO.getFirstRequestTime()) + "</td>");
html.append(" <td>" + tmpTO.getRequestTimes() + "次</td>");
html.append(" <td>" + getDateFormat(tmpTO.getLastLoadTime() + tmpTO.getExpire()) + "(" + tmpTO.getExpire()
html.append(" <td>" + getDateFormat(tmpTO.getLastLoadTime() + tmpTO.getExpire() * 1000) + "(" + tmpTO.getExpire()
+ "秒)</td>");
html.append(" <td>" + getDateFormat(tmpTO.getLastRequestTime() + tmpTO.getRequestTimeout()) + "("
html.append(" <td>" + getDateFormat(tmpTO.getLastRequestTime() + tmpTO.getRequestTimeout() * 1000) + "("
+ tmpTO.getRequestTimeout() + "秒)</td>");
html.append(" <td>" + getDateFormat(tmpTO.getLastLoadTime()) + "</td>");
html.append(" <td>" + tmpTO.getLoadCnt() + "次</td>");
Expand All @@ -179,23 +211,29 @@ private void printList(HttpServletResponse resp, ICacheManager<?> cacheManager)
+ "')\">移除 AutoloadTO</a></td>");
html.append(" <td><a href=\"javascript:void()\" onclick=\"resetLastLoadTime('" + tmpTO.getCacheKey()
+ "')\">重置最后加载时间</a></td>");
html.append("<td>");
if(null != tmpTO.getArgs() && tmpTO.getArgs().length > 0) {
html.append("<a href=\"" + req.getContextPath() + req.getServletPath() + "?act=showArgs&cacheManagerName="
+ cacheManagerName + "&cacheKey=" + tmpTO.getCacheKey() + "\" target=\"_blank\">show args values</a>");
}
html.append("</td>");
html.append(" </tr>");
}
html.append("</table>");

resp.getOutputStream().println(html.toString());
resp.getWriter().println(html.toString());
}

private void printCloseHtml(HttpServletResponse resp) throws IOException {
resp.getOutputStream().println("</body></html>");
resp.getWriter().println("</body></html>");
}

private String getDateFormat(long time) {
if(time < 100000) {
return "";
}
Date date=new Date(time);
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSS");
SimpleDateFormat df=new SimpleDateFormat("MM/dd/HH:mm:ss");
return df.format(date);
}
}
9 changes: 5 additions & 4 deletions src/main/java/com/jarvis/cache/redis/CachePointCut.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
byte[] key=redisTemplate.getStringSerializer().serialize(cacheKey);
JdkSerializationRedisSerializer serializer=(JdkSerializationRedisSerializer)redisTemplate.getValueSerializer();
byte[] val=serializer.serialize(result);
connection.set(key, val);
connection.expire(key, expire);
// connection.set(key, val);
// connection.expire(key, expire);
connection.setEx(key, expire, val);
return null;
}
});
Expand Down Expand Up @@ -124,15 +125,15 @@ public void deleteDefinedCacheKey(String keySpEL, Object[] arguments) {

/**
* 根据缓存Key删除缓存
* @param cacheKey 如果传进来的值中 带有 * 号,则会使用批量删除(遍历所有Redis服务器)
* @param cacheKey 如果传进来的值中 带有 * 或 ? 号,则会使用批量删除(遍历所有Redis服务器)
*/
@Override
public void delete(final String cacheKey) {
if(null == redisTemplateList || redisTemplateList.isEmpty()) {
return;
}
final AutoLoadHandler<Serializable> autoLoadHandler=this.getAutoLoadHandler();
if(cacheKey.indexOf("*") != -1) {
if(cacheKey.indexOf("*") != -1 || cacheKey.indexOf("?") != -1) {
for(final RedisTemplate<String, Serializable> redisTemplate: redisTemplateList) {
redisTemplate.execute(new RedisCallback<Object>() {

Expand Down

0 comments on commit b4047e8

Please sign in to comment.