/* ================================================================== * Created [2009-4-27 下午11:32:55] by Jon.King * ================================================================== * TSS * ================================================================== * mailTo:jinpujun@hotmail.com * Copyright (c) Jon.King, 2009-2012 * ================================================================== */ package com.jinhe.tss.component.cache; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.jinhe.tss.component.support.web.webwork.PTActionSupport; import com.jinhe.tss.core.cachepool.CacheManager; import com.jinhe.tss.core.cachepool.Cacheable; import com.jinhe.tss.core.cachepool.CacheableKey; import com.jinhe.tss.core.cachepool.strategy.CacheConstants; import com.jinhe.tss.core.cachepool.strategy.CacheStrategy; import com.jinhe.tss.core.util.BeanUtil; import com.jinhe.tss.core.util.XMLDocUtil; import com.jinhe.tss.core.web.dispaly.grid.DefaultGridNode; import com.jinhe.tss.core.web.dispaly.grid.GridDataEncoder; import com.jinhe.tss.core.web.dispaly.grid.IGridNode; import com.jinhe.tss.core.web.dispaly.tree.ITreeNode; import com.jinhe.tss.core.web.dispaly.tree.TreeAttributesMap; import com.jinhe.tss.core.web.dispaly.tree.TreeEncoder; import com.jinhe.tss.core.web.dispaly.xform.XFormEncoder; /** * <p> CacheStrategyAction.java </p> */ public class CacheStrategyAction extends PTActionSupport{ private static final long serialVersionUID = 7921364871206752664L; private String code; private String key; private CacheStrategy cacheStrategy = new CacheStrategy(); XMLCacheStrategyManager manager = XMLCacheStrategyManager.getInstance(); /** * 修改缓存策略。 * @return */ public String modifyCacheStrategy(){ manager.saveCacheStrategy(cacheStrategy); return printSuccessMessage(); } /** * 树型展示缓存策略 * @return */ public String getAllCacheStrategy4Tree(){ List<CacheStrategy> strategies = manager.getAllCacheStrategy(); List<ITreeNode> treeNodeList = new ArrayList<ITreeNode>(); for(final CacheStrategy stategy : strategies){ treeNodeList.add(new ITreeNode(){ public TreeAttributesMap getAttributes() { TreeAttributesMap map = new TreeAttributesMap(stategy.getCode(), stategy.getName()); map.put("icon", "images/cache.gif"); map.put("visible", stategy.getVisible()); map.put("released", stategy.getPoolInstance().isReleased() ? "1" : "0"); return map; } }); } TreeEncoder encoder = new TreeEncoder(treeNodeList); encoder.setNeedRootNode(false); return print("CacheTree", encoder); } /** * 获取缓存策略以及缓存池信息 * @return */ public String getCacheStrategyInfo(){ CacheStrategy strategy = manager.getCacheStrategy(code); Map<String, Object> strategyProperties = new HashMap<String, Object>(); BeanUtil.addBeanProperties2Map(strategy, strategyProperties); XFormEncoder xEncoder = new XFormEncoder(CacheConstants.CACHESTRATEGY_XFORM_TEMPLET, strategyProperties); String hitRate = CacheManager.getInstance().getCachePool(code).getHitRate() + "%"; List<Cacheable> cachedItems = manager.listItemsByStrategy(code); long requests = strategy.getPoolInstance().getRequests(); List<IGridNode> temp = new ArrayList<IGridNode>(); String name = null; for(Cacheable item : cachedItems){ int hit = item.getHit(); Object thisKey = item.getKey(); DefaultGridNode gridNode = new DefaultGridNode(); if(thisKey instanceof CacheableKey){ CacheableKey cKey = (CacheableKey) thisKey; gridNode.getAttrs().put("id", cKey.getKey()); gridNode.getAttrs().put("key", cKey.getKey()); gridNode.getAttrs().put("remark", cKey.getRemark()); name = cKey.getName(); }else{ gridNode.getAttrs().put("id", thisKey); gridNode.getAttrs().put("key", thisKey); } gridNode.getAttrs().put("code", code); gridNode.getAttrs().put("name", name); gridNode.getAttrs().put("hit", new Integer(hit)); gridNode.getAttrs().put("hitRate", ((requests == 0) ? 0 : (((float) hit / requests) * 100f)) + "%"); temp.add(gridNode); } StringBuffer template = new StringBuffer(); template.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><grid version=\"2\"><declare sequence=\"true\">"); template.append("<column name=\"id\" mode=\"string\" caption=\"项Id\" display=\"none\"/>"); template.append("<column name=\"code\" mode=\"string\" caption=\"池code\" display=\"none\"/>"); template.append("<column name=\"key\" caption=\"键值\" mode=\"string\"/>"); if(name != null) { template.append("<column name=\"name\" caption=\"名称\" mode=\"string\" align=\"center\"/>"); } template.append("<column name=\"hit\" caption=\"点击次数\" mode=\"string\" align=\"center\"/>"); template.append("<column name=\"hitRate\" caption=\"点击率\" mode=\"string\" align=\"center\"/>"); template.append("<column name=\"remark\" caption=\"说明\" mode=\"string\" align=\"center\"/>"); template.append("</declare><data></data></grid>"); GridDataEncoder gEncoder = new GridDataEncoder(temp, XMLDocUtil.dataXml2Doc(template.toString())); return print(new String[]{"CacheInfo", "CacheOption", "PageList", "PoolHitRate"}, new Object[]{xEncoder, gEncoder, createPageInfo(cachedItems.size()), hitRate}); } //加入分页信息,只有一页。 private String createPageInfo(int totalRows){ int currentPageRows = totalRows; int pagesize = totalRows + 1; StringBuffer sb = new StringBuffer("<pagelist totalpages=\"1\" totalrecords=\""); sb.append(totalRows).append("\" currentpage=\"1\" pagesize=\"").append(pagesize); sb.append("\" currentpagerows=\"").append(currentPageRows).append("\" />"); return sb.toString(); } /** * 刷新缓存 * @return */ public String refresh(){ Cacheable item = manager.refresh(code, key); if(item == null){ return printSuccessMessage("刷新成功,缓存项被清除出缓存。"); }else{ int hit = item.getHit(); long requests = manager.getCacheStrategy(code).getPoolInstance().getRequests(); String hitRate = ((requests == 0) ? 0 : (((float) hit / requests) * 100f)) + "%"; return print("CacheOption", "<row key=\"" + key + "\" hit=\"" + hit + "\" code=\"" + code + "\" hitRate=\"" + hitRate + "\"/>"); } } /** * 查看详细的缓存项内容。对象XML格式展示 * @return */ public String viewCachedItem(){ Cacheable item = manager.getCachedItem(code, key); if(item != null) { String returnStr = ""; try{ String valueStr = BeanUtil.toXml(item.getValue()); returnStr = XMLDocUtil.dataXml2Doc(valueStr).asXML(); } catch(Exception e) { returnStr = "缓存项(" + item.getValue() + ")内容生成XML不成功,无法查看!\n" + e.getMessage(); } print(returnStr); } else { print("该缓存项已经不存在,已经被清空或是已经被刷新!"); } return XML; } /** * 清空释放缓存池 * @return */ public String releaseCache(){ manager.releaseCache(code); return printSuccessMessage(); } /** * 初始化缓存池 * @return */ public String initPool(){ manager.initPool(code); return printSuccessMessage(); } public CacheStrategy getCacheStrategy() { return cacheStrategy; } public void setCode(String code) { this.code = code; } public void setKey(String key) { this.key = key; } }