ehcache用法的例子

1 篇文章 0 订阅
一种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>



 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值