像Spring Cache一样方便容易的使用缓存,本方案虽然从使用便利程度上说相对弱一些,但是从功能上说多了一个一级缓存性能层面大大提高。
方案采用j2cache:
1、
public class J2Cache implements ICache{ private String db ; private CacheChannel cache ; private Map<String,String> cacheExpire; private long defaultTimeToLiveInSeconds = 0;//默认不缓存 public J2Cache(String dbName,CacheChannel cache,Map<String,String> cacheExpire) { this.cache = cache; this.db = dbName; this.cacheExpire = cacheExpire; if(cacheExpire != null) this.defaultTimeToLiveInSeconds = expireValue2Long(cacheExpire.get("default")); } @Override public <T> T get(String cacheName, Object key) { // TODO Auto-generated method stub return (T)cache.get(db+"_"+cacheName, key.toString()).getValue(); } @Override public void put(String cacheName, Object key, Object value) { // TODO Auto-generated method stub if(cacheExpire != null) { long timeToLiveInSeconds = getTimeToLiveInSeconds(db+"_"+cacheName); if(timeToLiveInSeconds>0) cache.set(db+"_"+cacheName, key.toString(), value, timeToLiveInSeconds); }else cache.set(db+"_"+cacheName, key.toString(), value); } @Override public void remove(String cacheName, Object key) { // TODO Auto-generated method stub cache.evict(db+"_"+cacheName, key.toString()); } @Override public void removeAll(String cacheName) { // TODO Auto-generated method stub LogKit.info("clearn cache:" + db+"_"+cacheName); //cache.clear(db+"_"+cacheName);//清除直接卡死 cache.clear(cacheName); LogKit.info("clearn cache:%s end" , db+"_"+cacheName); } public long getTimeToLiveInSeconds(String cacheName) { String value = cacheExpire.get(cacheName); if(!xx.isEmpty(value)) { return expireValue2Long(value); } return defaultTimeToLiveInSeconds; } //xxxx[s|m|h|d]结尾 private long expireValue2Long(String expireValue) { long mstime = 0; expireValue = expireValue.trim(); try { if(expireValue.endsWith("s")) { mstime = Long.parseLong(expireValue.substring(0, expireValue.length()-1)); }else if(expireValue.endsWith("m")) { mstime = Long.parseLong(expireValue.substring(0, expireValue.length()-1))*60; }else if(expireValue.endsWith("h")) { mstime = Long.parseLong(expireValue.substring(0, expireValue.length()-1))*60*60; }else if(expireValue.endsWith("d")) { mstime = Long.parseLong(expireValue.substring(0, expireValue.length()-1))*24*60*60; } } catch (Exception e) { // TODO Auto-generated catch block //e.printStackTrace(); } return mstime; } }
2、JFinalConfig .configPlugin初始化
J2Cache mainCache = null; J2Cache bbCache = null; try { J2CacheConfig config = J2CacheConfig.initFromConfig("/j2cache.properties"); String l1CacheName = config.getL1CacheName(); Map<String,String> cacheExpire = null; if("none".equalsIgnoreCase(l1CacheName)) {//只用redis,需要自己管理redis失效 cacheExpire = new HashMap<String, String>(); Iterator<Entry<Object, Object>> it = config.getProperties().entrySet().iterator(); while (it.hasNext()) { Entry<Object, Object> entry = it.next(); String key = entry.getKey().toString(); String value = entry.getValue().toString(); cacheExpire.put(key, value); } } //填充 config 变量所需的配置信息 J2CacheBuilder builder = J2CacheBuilder.init(config); cache = builder.getChannel(); bbCache = new J2Cache(EovaConst.DS_EOVA,cache,cacheExpire); mainCache = new J2Cache(EovaConst.DS_MAIN,cache,cacheExpire); } catch (IOException e) { } DruidPlugin dp = initDruidPlugin(mainUrl, mainUser, mainPwd); ActiveRecordPlugin arp = initActiveRecordPlugin(mainUrl, xx.DS_MAIN, dp); System.out.println("load data source:" + mainUrl + "/" + mainUser); if(mainCache != null) arp.setCache(mainCache);
3、执行带缓存的查询方法即可
findByCache(cacheName, key, sql)
4、清除
cache.removeAll(cacheName); 和cache.remove(cacheName, key);
具体见项目:http://h.bblocks.cn/
喜欢的小伙伴给个赞呗~