缓存--ehcache3的基本使用

本文详细介绍 EHCache 3 的使用方法,包括如何配置和创建缓存实例,以及三种缓存方式的特点。提供了 Java 示例代码,展示了如何通过编程方式和 XML 配置文件来初始化 CacheManager 和缓存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.ehcache.org/documentation/3.7/getting-started.html   --cache3.7官方英文文档  怎么使用这里写的都很明白

https://www.cnblogs.com/zhao1949/p/8124325.html   springboot与ehcache3整合

https://www.jianshu.com/p/0a24a6ced3b9  springboot与ehcache3整合

ehcache3 和 ehcache2 完全是两个框架

 

导入包、需要有slf4j包的支持

<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>3.7.1</version>
</dependency>

Demo例子基本使用

其用法类似于使用map进行缓存,ehcache3帮我们封装了缓存策略、对象的序列化、硬盘文件io、持久化。使用者只需在配置好之后当成一个map使用。

package a.caixl.cache;

import java.io.File;

import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.UserManagedCache;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.builders.UserManagedCacheBuilder;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.impl.config.persistence.CacheManagerPersistenceConfiguration;

public class EhcacheDemo {

	public static void main(String[] args) {
		
		CacheManager cacheManager = 
				CacheManagerBuilder.newCacheManagerBuilder()
/**1**/		.with(new CacheManagerPersistenceConfiguration(new File("D:"+ File.separator + "myData")))
/**2**/		//.withCache("preConfigured",  cBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10))) 
				.build();
		
		cacheManager.init(); 
		
		/**解释
		 * CacheManager 由 静态的newCacheManagerBuilder创建
		 * 标记1:创建的时候可以绑定0-N个配置,使用with方法。如要使用硬盘缓存则必须指定存储地址
		 * 标的2:可初始就创建0-N个缓存,注意指定其别名,
		 * 创建缓存必须设定其CacheConfiguration
		 * **/
//		Cache<Long, String> preConfigured =  cacheManager.getCache("preConfigured", Long.class, String.class); 

		/**创建一个缓存,设定其配置 。 可以给 CacheConfiguration 绑定 缓存时效的配置 **/
		Cache<Long, String> myCache = cacheManager.createCache("myCache", 
		    CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, 
		    		ResourcePoolsBuilder.heap(10)  //堆内大小
		    		.offheap(2,MemoryUnit.MB )  //堆外大小
		    		.disk(1, MemoryUnit.GB)   //硬盘缓存大小
		    		)
//		    		.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(10)))		
		    );

		myCache.put(1L, "da one!"); 
		String value = myCache.get(1L); 
		System.out.println(value);
		
//		cacheManager.removeCache("preConfigured"); 

		cacheManager.close(); 
	}
}

ehcache3提供3种缓存方式
注意缓存的对象必须是实现了序列化接口,非堆内的缓存使用是序列化存储、反序列化读取。

堆内缓存:缓存在JVM中,也是最高效的并被GC管理的。
堆外缓存:缓存在JVM之外的内存中,通过对象系列化的方式缓存。  效率低于堆内缓存,比硬盘缓存快。
硬盘缓存:物理硬盘

实际的测试,缓存读取的速度非常快,读取硬盘缓存也是很快的。


使用xml配置得到CacheManager

<?xml version="1.0" encoding="UTF-8"?>

<config
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns='http://www.ehcache.org/v3'
    xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">

  <!-- 指定硬盘存储目录,如用不到硬盘缓存,无需设置 -->
  <persistence directory="D:/dataDir" />

  <cache alias="foo"> 
    <key-type>java.lang.String</key-type> 
    <value-type>java.lang.String</value-type> 
    <resources>
      <heap unit="entries">20</heap> 
      <offheap unit="MB">1</offheap> 
      <disk persistent="true" unit="MB">500</disk>
    </resources>
  </cache>

  <!-- cache-template  创建公开配置,被继承用 -->
  <cache-template name="myDefaults"> 
    <key-type>java.lang.Long</key-type>
    <value-type>java.lang.String</value-type>
    <heap unit="entries">200</heap>
  </cache-template>

  <!-- 继承自myDefaults,并重写其参数 -->
  <cache alias="bar" uses-template="myDefaults"> 
    <key-type>java.lang.Number</key-type>
  </cache>

  <cache alias="simpleCache" uses-template="myDefaults" /> 

</config>
package a.caixl.cache;

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

import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.Configuration;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.xml.XmlConfiguration;

public class EhcacheXmlDemo {
	public static void main(String[] args) throws MalformedURLException {
		URL myUrl =EhcacheXmlDemo.class.getResource("/ehcache-config.xml");
		Configuration xmlConfig = new XmlConfiguration(myUrl); 
		CacheManager myCacheManager = CacheManagerBuilder.newCacheManager(xmlConfig); 
		myCacheManager.init(); //注意需要进行初始化
		
		Cache<String, String> foo = myCacheManager.getCache("foo", String.class, String.class); 
		
		Cache<Number, String> bar = myCacheManager.getCache("bar", Number.class, String.class); 
		
		Cache<Long, String> simpleCache = myCacheManager.getCache("simpleCache", Long.class, String.class); 
		
		System.out.println(foo);
		System.out.println(bar);
		System.out.println(simpleCache);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值