ehcache 使用方法
引用 :http://blog.csdn.net/zk_zhangkai/article/details/6921924
- 一种ehcache实现缓存的方式
- /**
- *
- */
- package com.datang.common.cache.flat;
- import java.net.URL;
- import java.util.List;
- import net.sf.ehcache.CacheManager;
- import net.sf.ehcache.Ehcache;
- import net.sf.ehcache.Element;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.datang.common.tools.util.ResourceUtils;
- /**
- * 平面缓存,按缓存名区分缓存,key-value结构
- *
- */
- public abstract class FlatCache {
- /**
- * 日志
- */
- private static final Logger LOGGER = LoggerFactory
- .getLogger(FlatCache.class);
- /**
- * 缓存配置文件
- */
- private static String CACHE_CONFIG_FILE = "config/flat_cache.xml";
- /**
- * Ehanche的缓存管理
- */
- private static CacheManager cacheManager = null;
- /**
- * 设置缓存配置文件,最开始设置才有效果,一旦缓存加载则不能改变
- *
- * @param cacheConfigFile
- */
- public static void setCacheConfigFile(String cacheConfigFile) {
- CACHE_CONFIG_FILE = cacheConfigFile;
- }
- /**
- * 按缺省配置创建缓存
- *
- * @param cacheName
- */
- public static void createCache(String cacheName) {
- getCacheManager().addCache(cacheName);
- }
- /**
- * 添加缓存
- *
- * @param cacheName
- * @param key
- * @param value
- */
- public static void put(String cacheName, String key, Object value) {
- Ehcache cache = getCacheManager().getEhcache(cacheName);
- cache.put(new Element(key, value));
- }
- /**
- * 根据缓存名与key获取值
- *
- * @param cacheName
- * @param key
- * @return
- */
- public static Object get(String cacheName, String key) {
- Ehcache cache = getCacheManager().getEhcache(cacheName);
- Element e = cache.get(key);
- return e == null ? null : e.getObjectValue();
- }
- /**
- * 获取缓存名
- *
- * @return
- */
- public static String[] getCacheNames() {
- return getCacheManager().getCacheNames();
- }
- /**
- * 获取缓存的Keys
- *
- * @param cacheName
- * @return
- */
- @SuppressWarnings("unchecked")
- public static List<String> getKeys(String cacheName) {
- Ehcache cache = getCacheManager().getEhcache(cacheName);
- return (List<String>) cache.getKeys();
- }
- /**
- * 清除所有
- */
- public static void clearAll() {
- getCacheManager().clearAll();
- }
- /**
- * 清空指定缓存
- *
- * @param cacheName
- */
- public static void clear(String cacheName) {
- getCacheManager().getCache(cacheName).removeAll();
- }
- /**
- * 删除指定对象
- *
- * @param cacheName
- * @param key
- * @return
- */
- public static boolean remove(String cacheName, String key) {
- return getCacheManager().getCache(cacheName).remove(key);
- }
- /**
- * 获取缓存大小
- *
- * @param cacheName
- * @return
- */
- public static int getSize(String cacheName) {
- return getCacheManager().getCache(cacheName).getSize();
- }
- /**
- * 获取CacheManager
- *
- * @return
- */
- private static CacheManager getCacheManager() {
- if (cacheManager != null) {
- return cacheManager;
- }
- try {
- URL url = ResourceUtils.getResource(CACHE_CONFIG_FILE);
- cacheManager = CacheManager.create(url);
- } catch (RuntimeException e) {
- LOGGER.error("init flat cache failed", e);
- throw e;
- }
- return cacheManager;
- }
- }
xml的配置如下:
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="ehcache.xsd"
- updateCheck="true" monitoring="autodetect"
- dynamicConfig="true">
- <defaultCache
- maxElementsInMemory="20"
- eternal="false"
- overflowToDisk="false"
- timeToIdleSeconds="1800"
- timeToLiveSeconds="1800">
- </defaultCache>
- <cache name="CDG-CACHE"
- maxElementsInMemory="20"
- overflowToDisk="false"
- eternal="false"
- timeToIdleSeconds="1800"
- timeToLiveSeconds="1800"
- memoryStoreEvictionPolicy="LRU"
- transactionalMode="off"
- />
- </ehcache>