/** Copyright 2009 by primeton Corporation. * * All rights reserved. * * This software is the confidential and proprietary information of * primeton Corporation ('Confidential Information'). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with primeton. */ package com.yiidata.intergration.web.modules.sys.cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import net.sf.ehcache.search.Attribute; import net.sf.ehcache.search.Query; import net.sf.ehcache.search.Result; import net.sf.ehcache.search.Results; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.Closeable; import java.io.IOException; import java.net.URL; import java.util.List; /** * @author xiongbin * @Version 2012-9-4 */ public class Ehcache implements ICache, Closeable { private static Logger logger = LoggerFactory.getLogger(Ehcache.class); /** * EHCache */ private final net.sf.ehcache.Ehcache cache; public Ehcache(){ URL resource = Ehcache.class.getResource("/ehcache.xml"); CacheManager manager = CacheManager.create(resource); net.sf.ehcache.Ehcache ehcache = manager.getCache("Cache"); if(ehcache==null){ manager.addCache("Cache"); ehcache = manager.getEhcache("Cache"); } this.cache = ehcache; } /* (non-Javadoc) * @see com.primeton.dgs.kernel.common.cache.ICache#add(java.lang.String, java.lang.Object) */ @Override public void add(String key, Object value) { Element element = new Element(key, value); cache.put(element); } /* (non-Javadoc) * @see com.primeton.dgs.kernel.common.cache.ICache#add(java.lang.String, java.lang.Integer, java.lang.Object) */ @Override public void add(String key, int exp, Object value) { Element element = new Element(key, value); element.setTimeToLive(exp); cache.put(element); } /* (non-Javadoc) * @see com.primeton.dgs.kernel.common.cache.ICache#get(java.lang.String) */ @Override public Object get(String key) { Element element = cache.get(key); if(element != null){ return element.getValue(); } return null; } /** * 移除某条缓存 * @param key 缓存的 KEY * @return */ @Override public Object remove(String key) { Element element = cache.get(key); cache.remove(key); logger.info("remove cache key: {}", key); if(element != null){ return element.getObjectValue(); } return null; } @Override public int removeByPrefix(String prefix) { Attribute keySearch = cache.getSearchAttribute("key"); //创建一个用于查询的Query对象 Query query = cache.createQuery(); //给当前query添加一个筛选条件——可查询属性name的值等于“name1” query.addCriteria(keySearch.ilike(prefix+"*")); // ehcache 模糊删除 query.includeAttribute(keySearch); query.includeKeys(); Results result = query.execute(); List keys = result.all(); for (Result key : keys) { remove((String)key.getKey()); } return result.size(); } /* (non-Javadoc) * @see com.primeton.dgs.kernel.common.cache.ICache#clear() */ @Override public void clear() { cache.removeAll(); } /** * 获得缓存队列的长度 * @return 返回大于等于 0 则代表真实长度,返回 -1 代表无法获得 */ @Override public int size() { return cache.getSize(); } @Override public void shutdown() { try { close(); } catch (IOException e) { } } @Override public void close() throws IOException { cache.flush(); } }