EhCache缓存学习

在看hibernate的官方文档时,看到关于缓存的介绍。

hibernate在缓存管理上做的很好,具体使用,本章不做讲解,本篇主要研究EhCache的用法。其中hibernate使用到的缓存提供商列表如下:

Cache Provider class Type Cluster Safe Query Cache Supported
Hashtable (not intended for production use) org.hibernate.cache.HashtableCacheProvider memory   yes
EHCache org.hibernate.cache.EhCacheProvider memory, disk, transactional, clustered yes yes
OSCache org.hibernate.cache.OSCacheProvider memory,disk   yes
SwarmCache org.hibernate.cache.SwarmCacheProvider clustered (ip multicast) yes (clustered invalidation)  
JBoss Cache 1.x org.hibernate.cache.TreeCacheProvider clustered (ip multicast), transactional yes (replication) yes (clock sync req.)
JBoss Cache 2 org.hibernate.cache.jbc.JBossCacheRegionFactory clustered (ip multicast), transactional yes (replication or invalidation) yes (clock sync req.)

其中,我对EHCache比较感兴趣。看它支持的类型包括对内存,硬盘,传统,集群都支持。

我们可以单独研究一下Ehcache缓存的使用,这样方便以后我们对其他使用到缓存的地方进行缓存的自定义管理(不单单在hibernate查询数据方面)。


ehcache下载地址:   http://sourceforge.net/projects/ehcache/files/ehcache/  

先写个例子,看看它的api如何使用:


EhcacheTest

package org.base.cache.test;

import java.net.MalformedURLException;
import java.net.URL;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;

/**
 * Ehcache缓存管理的api测试小例子
 * @author lushuaiyin
 *
 */
public class EhcacheTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws MalformedURLException {
		net.sf.ehcache.config.Configuration config=new Configuration();
		
		//如果不使用ehcache.xml配置文件,那么必须用代码配置一个defaultCacheConfiguration
		CacheConfiguration defaultCacheConfiguration=new CacheConfiguration();
		defaultCacheConfiguration.setMaxEntriesLocalHeap(0);
		defaultCacheConfiguration.setEternal(false);
		defaultCacheConfiguration.setTimeToIdleSeconds(30);
		defaultCacheConfiguration.setTimeToLiveSeconds(30);
		
		config.addDefaultCache(defaultCacheConfiguration);//设置默认cache
		
		net.sf.ehcache.CacheManager cacheManager=CacheManager.create(config);
		
		//创建缓存信息
		/*构造方法有多种,详见文档
		 public Cache(String name,
             int maxElementsInMemory,
             boolean overflowToDisk,
             boolean eternal,
             long timeToLiveSeconds,
             long timeToIdleSeconds)
		 */
		//自定义配置缓存
		net.sf.ehcache.Cache cache1=new Cache("mycache-one", 1000
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值