【EhCache一】EhCache版Hello World

首先到ehcache官网下载:ehcache官网

下载完后解压。

打开Myeclipse 创建web项目,然后在项目classpath根目录下创建ehchache.xml

==”autodetect”dynamicConfig=”true”><defaultCache======”LRU”transactionalMode=”off”> ></ehcache>

参数含义:

name:Cache的唯一标识,程序中使用这个名字获得缓存实例

maxElementsInMemory:内存中最大缓存对象数 。

overflowToDisk:当overflowToDisk配置为true,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。

eternal:Element是否永久有效,一但设置了, timeToIdleSeconds和 timeToLiveSeconds 将不起作用

timeToIdleSeconds: Element在缓存中空闲的最大时间。也就是说,这个时间控制的是一个Element 在一直没有被访问的前提下,这个对象可以在cache中的存活时间。若是0,表示永远存活

timeToLiveSeconds: 设置对象在cache中的最大存活时间,就是 无论对象被访问或是闲置,这个对象在cache中总的存活时间。也就是说, timeToLiveSeconds的值得应该大于等于 timeToIdleSeconds, 若是0,表示永远存活。

memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内 存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)

maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。 该选项只有在overflowToDisk为true时有效

diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区

EhCache CRUD例子

package import netimport netimport netimport netimport netimport netimport netimport netimport orgimport orgimport javaimport javaimport javapublic class EhcacheTest {@Testpublic void testCacheManager() { //加载EhCache配置文件 InputStream in = EhcacheTest.class.getClassLoader().getResourceAsStream(“ehcache.xml”); CacheManager cm = CacheManager.create(in); //列出所有的缓存名称,不包括配置文件中的<defaultCache> String[] names = cm.getCacheNames(); //只有一个名称为data-cache的cache Assert.assertEquals(1, names.length); Assert.assertEquals(names[0], “data-cache”); //根据指定名称查找缓存对象 Cache cache = cm.getCache(“data-cache”); //根据缓存名称获取缓存 Assert.assertNotNull(cache); //获取,,更新Cache配置的接口// CacheConfiguration configuration = cache.getCacheConfiguration();// configuration.setTimeToIdleSeconds(3600); //缓存在内存中的配置信息,缓存配置动态修改也会体现出来, System.out.println(cm.getActiveConfigurationText()); //清除所有缓存的数据,但是缓存本身仍然存在// cm.clearAll(); //从内存中删除一个缓存以及所有的数据,Cache被销毁// cm.removeCache(“data-cache”);}@Testpublic void testCache() { //只有被有初始化生效的Cache允许改名字 //cache.setName(“data-cache-changed”);}@Testpublic void testAddElementToCache() { //加载EhCache配置文件 InputStream in = EhcacheTest.class.getClassLoader().getResourceAsStream(“ehcache.xml”); CacheManager cm = CacheManager.create(in); Cache cache = cm.getCache(“data-cache”); Person p1 = new Person(1, “Jack”, 21); Person p2 = new Person(2, “Mike”, 73); cache.putIfAbsent(new Element(p1,p1, 1)); cache.put(new Element(p2,p2, 1)); cache.putIfAbsent(new Element(p2, p1,1));//只有Key为p2的数据不存在才插入 //得到的是p2,而不是p1 Element e = cache.get(p2); Assert.assertEquals(p2, e.getObjectValue()); //把数据从内存刷到DiskStore,从DiskStore刷新到Disk中 cache.flush();}@Testpublic void testRemoveElementFromCache() { //加载EhCache配置文件 InputStream in = EhcacheTest.class.getClassLoader().getResourceAsStream(“ehcache.xml”); CacheManager cm = CacheManager.create(in); Cache cache = cm.getCache(“data-cache”); Person p1 = new Person(1, “Jack”, 21); Person p2 = new Person(2, “Mike”, 73); Element e1 = new Element(p1,p1, 1); cache.putIfAbsent(e1); Element e2 = new Element(p2,p2, 1); cache.put(e2); cache.remove(p1); boolean isSucc = cache.removeElement(e1); //e1已经被删除,因此操作返回false Assert.assertFalse(isSucc); cache.put(e1); cache.removeAll(); Assert.assertEquals(0, cache.getSize());}@Testpublic void testUpdateElementInCache() { //加载EhCache配置文件 InputStream in = EhcacheTest.class.getClassLoader().getResourceAsStream(“ehcache.xml”); CacheManager cm = CacheManager.create(in); Cache cache = cm.getCache(“data-cache”); Person p1 = new Person(1, “Jack”, 21); Person p2 = new Person(2, “Mike”, 73); Element e1 = new Element(p1,p1, 1); cache.putIfAbsent(e1); Element e2 = new Element(p2,p2, 1); cache.put(e2); e2 = new Element(p2, p1, 1); cache.replace(e2); Assert.assertEquals(p1, e2.getObjectValue());}@Testpublic void testQueryElementsFromCache() { InputStream in = EhcacheTest.class.getClassLoader().getResourceAsStream(“ehcache.xml”); CacheManager cm = CacheManager.create(in); Cache cache = cm.getCache(“data-cache”); //EhCache中的数据类型是Element,它包含Key,Value和一个版本信息 Element e = new Element(1000, 10000, 1); cache.put(e); //添加第二个数据 e = new Element(2000, 20000, 1); cache.put(e); //缓存中有两条数据 Assert.assertEquals(2, cache.getSize()); //通过get方法获得key对应的数据 e = cache.get(1000); Assert.assertEquals(10000, e.getObjectValue()); //创建查询 Query q = cache.createQuery(); //Cache没有配置任何查询属性,这里察看下默认的查询属性有哪些 //set中包含两个可查询元素:key和value Set<Attribute> set = cache.getSearchAttributes(); //属性是范型类,得到key都应的查询属性对象 Attribute<Integer> keyAttribute = cache.getSearchAttribute(“key”); //根据默认提供的可查询属性key进行查询 //构造查询条件,这是一个链式写法,一个Query对象可以写多个查询条件 //创建查找key的值为2000的查询 q = q.addCriteria(keyAttribute.eq(2000)); //如果不includeKeys和q.includeValues();,则测试结果集中不包括Keys和Values信息 q.includeKeys(); q.includeValues(); //执行查询 Results results = q.execute();//执行查询 Assert.assertNotNull(results); Assert.assertEquals(results.size(), 1); //列出所有结果 List<Result> resultList = results.all(); Result result = resultList.get(0); Assert.assertEquals(2000, result.getKey()); Assert.assertEquals(20000, result.getValue());}}怕走崎岖路,莫想登高峰。

【EhCache一】EhCache版Hello World

相关文章:

你感兴趣的文章:

标签云: