wordpress建站教程jiuyou网站建站报价单
- 作者: 多梦笔记
- 时间: 2026年02月18日 22:07
当前位置: 首页 > news >正文
wordpress建站教程jiuyou,网站建站报价单,网站建设首选易网宣,加强网站微信信息编辑队伍建设目录 1、Ehcache 简介2、Ehcache 集群方式3、工作原理3.1、缓存写入3.2、缓存查找3.3、缓存过期和驱逐3.4、缓存持久化 4、入门案例 —— Ehcache 2.x 版本4.1、单独使用 Ehcache4.1.1、引入依赖4.1.2、配置 Ehcache4.1.2.1、XML 配置方式4.1.2.1.1、新建 ehcache.xml4.1.2.1.2… 目录 1、Ehcache 简介2、Ehcache 集群方式3、工作原理3.1、缓存写入3.2、缓存查找3.3、缓存过期和驱逐3.4、缓存持久化 4、入门案例 —— Ehcache 2.x 版本4.1、单独使用 Ehcache4.1.1、引入依赖4.1.2、配置 Ehcache4.1.2.1、XML 配置方式4.1.2.1.1、新建 ehcache.xml4.1.2.1.2、使用 Ehcache 4.1.2.2、API 配置方式4.1.2.2.1、添加一个配置类4.1.2.2.2、使用 Ehcache 5、入门案例 —— Ehcache 3.x 版本5.1、单独使用 Ehcache5.1.1、引入依赖5.1.2、配置 Ehcache5.1.2.1、XML 文件配置5.1.2.1.1、新建 ehcache.xml 文件5.1.2.1.2、使用 Ehcache 5.1.2.2、API 配置5.1.2.2.1、新建一个配置类5.1.2.2.2、使用 EhCache5.1.2.2.3、EhCache 数据存储位置5.1.2.2.4、数据生存时间 5.2、在 SpringBoot 中使用 Ehcache5.2.1、引入依赖5.2.2、配置 Ehcache5.2.2.1、XML 配置文件5.2.2.1.1、新建一个配置文件5.2.2.1.2、启动类开启缓存注解5.2.2.1.3、添加配置类5.2.2.1.4、使用 Ehcache 5.2.2.2、API 配置5.2.2.2.1、新建一个配置类5.2.2.2.2、使用 Ehcache 5.3、集成 Spring Cache5.3.1、引入依赖5.3.2、启动类开启缓存注解5.3.3、配置 Ehcache5.3.3.1、添加配置信息5.3.3.2、配置属性类5.3.3.3、配置 CachaManager 5.3.4、使用 Ehcache5.3.4.1、基于注解的缓存5.3.4.1.1、新建一个 UserServiceImpl5.3.4.1.2、新建一个 TestController 5.3.4.12、基于缓存管理器的编程方式 1、Ehcache 简介
EhCache 是一种广泛使用的开源 Java 分布式缓存。主要面向通用缓存、Java EE 和轻量级容器可以和大部分 Java 项目无缝整合。 Ehcache 虽然也支持分布式模式但是分布式方案不是很好建议只将其作为单机的进程内缓存使用 特点
直接在 JVM 虚拟机中缓存速度快效率高支持多种缓存策略LRU、LFU、FIFO 淘汰算法支持内存和磁盘存储默认存储在内存中如内存不够时把缓存数据同步到磁盘中支持多缓存管理器实例以及一个实例的多个缓存区域支持基于 Filter 的 Cache 实现也支持 Gzip 压缩算法EhCache 可以单独使用一般在第三方库中被用到的比较多【mybatis、shiro】EhCache 对分布式支持不够好多个节点通过组播方式同步效率不高通常和 Redis 一块使用【通过 RMI 或者 Jgroup 多播方式进行广播缓存通知更新缓存共享复杂维护不方便简单的共享可以但是涉及到缓存恢复大数据缓存则不合适】
2、Ehcache 集群方式
Ehcache 目前支持五种集群方式
RMI使用组播方式通知所有节点同步数据。如果网络有问题或某台服务宕机则存在数据无法同步的可能导致数据不一致。JMS。JMS 类似 MQ所有节点订阅消息当某节点缓存发生变化就向 JMS 发消息其他节点感知变化后同步数据JGroupTerracottaEhcache Server
3、工作原理
3.1、缓存写入
当应用程序向 Ehcache 中写入数据时Ehcache 会首先检查该数据是否已经存在于缓存中。如果数据已经存在于缓存中则更新该数据否则将该数据写入缓存。如下
当应用程序请求写入一个数据项到 Ehcache 中时这个数据项被传递给Ehcache APIEhcache 首先根据该数据项的键值对定位其对应的 Cache 对象Ehcache 根据配置中的缓存策略比如是否应该在缓存中创建一个新的元素以及新元素是否会淘汰老的元素如果需要创建一个新缓存元素则 Ehcache 创建一个新的元素并将其添加到 Cache 对象中如果缓存中已经存在该元素Ehcache 会根据缓存策略对该元素进行更新或替换Ehcache 将更新后的 Cache 对象返回给应用程序
3.2、缓存查找
当应用程序需要查询缓存中的数据时Ehcache 首先会检查该数据是否存在于缓存中。如果数据存在于缓存中则直接返回缓存数据否则从数据库或其他资源获取数据并将其存入缓存中。如下Ehcache 缓存查找的详细流程。
当应用程序请求从 Ehcache 中读取一个数据项时这个请求被传递给 Ehcache API。Ehcache 首先根据该数据项的键值对定位其对应的 Cache 对象。Ehcache 检查该数据项是否已经存在于缓存中。如果数据项存在于缓存中Ehcache 可以直接将其返回给应用程序。如果数据项不存在于缓存中Ehcache 就需要从数据库或其他数据源如文件、网络等中获取数据。获取到数据后Ehcache 会将其添加到缓存中并返回给应用程序
3.3、缓存过期和驱逐
Ehcache 提供了多种缓存失效策略例如基于时间的缓存失效、基于访问的缓存失效、基于大小的缓存失效等。当缓存数据过期或缓存空间不足时Ehcache 会选择一部分缓存元素进行驱逐以腾出更多的内存空间。如下
Ehcache 会周期性地扫描缓存中的元素来标记那些已经过期的元素。Ehcache根据缓存策略如基于时间、基于访问、基于大小等判断哪些缓存元素应该被驱逐。驱逐过程通常是异步执行的Ehcache 会在后台线程上执行这个操作
3.4、缓存持久化
Ehcache 还提供了缓存持久化功能它可以将缓存中的数据持久化到磁盘或者其他数据源。在应用程序重启或者缓存失效后Ehcache 可以从持久化存储中恢复数据从而保证数据的完整性和可靠性。如下
Ehcache 使用磁盘存储或数据库等持久化技术来存储缓存数据。当缓存中的数据更新时Ehcache 会自动将此数据持久化到持久化存储中。在应用程序重启或者缓存失效后Ehcache 会从持久化存储中读取缓存数据并重新加载到内存中
4、入门案例 —— Ehcache 2.x 版本
EhCache缓存框架
4.1、单独使用 Ehcache
4.1.1、引入依赖
dependencygroupIdnet.sf.ehcache/groupIdartifactIdehcache/artifactIdversion2.10.6/version
/dependency4.1.2、配置 Ehcache
配置 Ehcache用于定义缓存的行为和属性。
以下是一些主要用途
缓存定义: 可以定义一个或多个缓存包括它们的名称、存储策略如内存、磁盘、最大条目数等过期策略: 你可以设置缓存条目的过期时间如“时间到期”或“空闲时间”。这有助于控制缓存数据的生命周期溢出到磁盘: 如果缓存超出了内存限制可以配置将数据溢出到磁盘以避免数据丢失持久化选项: 配置是否需要将缓存数据持久化到文件系统以便在应用重启后恢复缓存状态性能优化: 可以为缓存设置特定的性能参数以优化应用的缓存行为
配置 Ehcache 有两种方式分别为 XML、API
4.1.2.1、XML 配置方式
4.1.2.1.1、新建 ehcache.xml
ehcache.xml是 EhCache 的配置文件。通过这个文件可以指定多个缓存的设置以及全局的默认配置。
在 classpath 路径下新建 ehCache/ehcache.xml 文件内容如下
?xml version1.0 encodingUTF-8?
ehcache!–磁盘的缓存位置磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存path:指定在硬盘上存储对象的路径可以配置的目录有user.home用户的家目录user.dir用户当前的工作目录java.io.tmpdir默认的临时目录ehcache.disk.store.direhcache 的配置目录绝对路径如d:\ehcache查看路径方法String tmpDir System.getProperty(java.io.tmpdir);–diskStore pathD:/ehCache/data/!–默认缓存–!–defaultCache默认缓存策略当 ehcache 找不到定义的缓存时则使用这个缓存策略。只能定义一个。–defaultCachemaxEntriesLocalHeap10000eternalfalsetimeToIdleSeconds120timeToLiveSeconds120maxEntriesLocalDisk10000000diskExpiryThreadIntervalSeconds120memoryStoreEvictionPolicyLRUpersistence strategylocalTempSwap//defaultCache!–userCache 缓存–!–name:缓存名称。maxElementsInMemory:缓存最大数目maxElementsOnDisk硬盘最大缓存个数。eternal:对象是否永久有效一但设置了timeout 将不起作用。overflowToDisk:是否保存到磁盘当系统宕机时timeToIdleSeconds:设置对象在失效前的允许闲置时间单位秒。仅当 eternalfalse 对象不是永久有效时使用可选属性默认值是 0也就是可闲置时间无穷大。timeToLiveSeconds:设置对象在失效前允许存活时间单位秒。最大时间介于创建时间和失效时间之间。仅当 eternalfalse 对象不是永久有效时使用默认是 0.也就是对象存活时间无穷大。diskPersistent是否缓存虚拟机重启期数据默认为 falsediskSpoolBufferSizeMB这个参数设置 DiskStore磁盘缓存的缓存区大小。默认是 30MB。每个 Cache 都应该有自己的一个缓冲区。diskExpiryThreadIntervalSeconds磁盘失效线程运行时间间隔默认是120 秒。memoryStoreEvictionPolicy当达到 maxElementsInMemory 限制时Ehcache 将会根据指定的策略去清理内存。默认策略是 LRU最近最少使用。你可以设置为 FIFO先进先出或是 LFU较少使用。clearOnFlush内存数量最大时是否清除。memoryStoreEvictionPolicy:可选策略有LRU最近最少使用默认策略、FIFO先进先出、LFU最少访问次数。FIFOfirst in first out这个是大家最熟的先进先出。LFU Less Frequently Used就是上面例子中使用的策略直白一点就是讲一直以来最少被使用的。如上面所讲缓存的元素有一个 hit 属性hit 值最小的将会被清出缓存。LRULeast Recently Used最近最少使用的缓存的元素有一个时间戳当缓存容量满了而又需要腾出地方来缓存新的元素的时候那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。–cache nameuserCachemaxElementsInMemory1000eternalfalsetimeToIdleSeconds5timeToLiveSeconds5overflowToDiskfalsememoryStoreEvictionPolicyLRU/
/ehcache配置说明
diskStore用于配置缓存的磁盘存储路径。所有被溢出到磁盘的缓存数据将会存储在这个目录中 数据持久化当内存中的缓存条目超过限制时EhCache 会将一些条目溢出到指定的磁盘路径以减少内存使用并保持应用性能恢复能力如果配置了持久化策略可以在应用重启后从磁盘恢复缓存数据确保数据不会丢失 defaultCache默认缓存策略。当 Ecache 找不到定义的缓存时则使用这个缓存策略。只能定义一个 maxEntriesLocalHeap10000指定在本地内存Heap中可以存储的最大条目数。这里设置为 10000表示最多可以存放 10,000 个缓存条目eternalfalse缓存条目是否永远有效。设置为 false意味着条目会在指定的时间后失效timeToIdleSeconds120指定条目在不被访问的情况下可以保持有效的时间单位为秒。如果条目在 120 秒内未被访问将会被移除timeToLiveSeconds120指定条目从被创建开始到失效的总时间单位为秒。如果条目在 120 秒后不论是否被访问都会被移除maxEntriesLocalDisk10000000指定在本地磁盘中可以存储的最大条目数。设置为 10000000表示最多可以在磁盘上存放 10,000,000 个条目diskExpiryThreadIntervalSeconds120指定处理磁盘过期条目的线程运行的间隔单位为秒。设置为 120表示每 120 秒检查一次磁盘中的过期条目memoryStoreEvictionPolicyLRU指定内存存储的驱逐策略。在这里设置为 LRULeast Recently Used表示当内存达到最大容量时会优先移除最近最少使用的条目persistence strategylocalTempSwap用于配置缓存的持久化策略。strategy“localTempSwap” 表示将缓存数据临时存储到本地磁盘以防数据丢失但不一定是永久存储 userCache
4.1.2.1.2、使用 Ehcache
public class Test {public static void main(String[] args) {// 获取 EhCache 的缓存管理对象CacheManager cacheManager new CacheManager(Test.class.getClassLoader().getResourceAsStream(ehcache/ehcache.xml));Cache cache cacheManager.getCache(userCache);// 创建缓存数据Element element new Element(name,zzc);// ① 存入缓存cache.put(element);// ② 从缓存中取出Element element1 cache.get(name);System.out.println(element1.getObjectValue());// ③ 删除缓存boolean flag cache.remove(name);if (flag) {System.out.println(删除成功);} else {// ④ 再次获取缓存数据element1 cache.get(name);System.out.println(element1.getObjectValue());}cacheManager.shutdown();}
}可以使用 put、get、remove 等方法进行缓存操作
4.1.2.2、API 配置方式
4.1.2.2.1、添加一个配置类
public class EhCacheConfig {private CacheManager cacheManager;public EhCacheConfig() {// 创建全局配置Configuration configuration new Configuration();// 配置缓存CacheConfiguration cacheConfig new CacheConfiguration(userCache, 1000) // 缓存名称和最大条目数.memoryStoreEvictionPolicy(LRU) // 驱逐策略.timeToLiveSeconds(300) // 存活时间.timeToIdleSeconds(300); // 空闲时间configuration.addCache(cacheConfig);// 初始化 CacheManagercacheManager CacheManager.newInstance(configuration);}public Cache getCache(String cacheName) {return cacheManager.getCache(cacheName);}public void shutdown() {if (cacheManager ! null) {cacheManager.shutdown();}}
}4.1.2.2.2、使用 Ehcache
public class Test {public static void main(String[] args) {// 获取 EhCache 的缓存管理对象EhCacheConfig ehCacheConfig new EhCacheConfig();Cache cache ehCacheConfig.getCache(userCache);// 创建缓存数据Element element new Element(name,zzc);// ① 存入缓存cache.put(element);// ② 从缓存中取出Element element1 cache.get(name);System.out.println(element1.getObjectValue());// ③ 删除缓存boolean flag cache.remove(name);if (flag) {System.out.println(删除成功);} else {// ④ 再次获取缓存数据element1 cache.get(name);System.out.println(element1.getObjectValue());}ehCacheConfig.shutdown();}
}5、入门案例 —— Ehcache 3.x 版本
5.1、单独使用 Ehcache
5.1.1、引入依赖
dependencygroupIdorg.ehcache/groupIdartifactIdehcache/artifactIdversion3.10.0/version
/dependency5.1.2、配置 Ehcache
5.1.2.1、XML 文件配置
5.1.2.1.1、新建 ehcache.xml 文件
config xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlnshttp://www.ehcache.org/v3xsi:schemaLocationhttp://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsdcache aliasuserCachekey-typejava.lang.Integer/key-typevalue-typejava.lang.String/value-typeexpiry!– 设置过期时间为 5 分钟 –ttl unitminutes5/ttl /expiryresourcesheap unitentries100/heap !– 最大条目数 –/resources/cache
/config定义了一个名为 userCache 的缓存配置了键值类型、过期时间、最大存储数量等参数
5.1.2.1.2、使用 Ehcache
public class Test {public static void main(String[] args) {// 获取 EhCache 的缓存管理对象XmlConfiguration xmlConfig new XmlConfiguration(Test.class.getResource(/ehcache/ehcache.xml));CacheManager cacheManager CacheManagerBuilder.newCacheManager(xmlConfig);cacheManager.init();CacheInteger, String userCache cacheManager.getCache(userCache, Integer.class, String.class);// ① 存入缓存userCache.put(1, zzc);// ② 从缓存中取出String name userCache.get(1);System.out.println(name);// ③ 删除缓存userCache.remove(1);System.out.println(删除成功);// ④ 再次获取缓存数据name userCache.get(1);System.out.println(name);cacheManager.close();}
}5.1.2.2、API 配置
5.1.2.2.1、新建一个配置类
public class EhCacheConfig {private CacheManager cacheManager;public EhCacheConfig() {this.cacheManager CacheManagerBuilder.newCacheManagerBuilder().withCache(userCache,CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class,String.class,ResourcePoolsBuilder.newResourcePoolsBuilder()// 最大条目数.heap(100).build()).build())// 初始化 CacheManager.build(true);}public CacheInteger, String getCache(String cacheName) {return cacheManager.getCache(cacheName, Integer.class, String.class);}public void close() {if (cacheManager ! null) {cacheManager.close();}}
}5.1.2.2.2、使用 EhCache
public class Test {public static void main(String[] args) {EhCacheConfig cacheConfig new EhCacheConfig();CacheInteger, String userCache cacheConfig.getCache(userCache);// ① 存入缓存userCache.put(1, zzc);// ② 从缓存中取出String name userCache.get(1);System.out.println(name);// ③ 删除缓存userCache.remove(1);System.out.println(删除成功);// ④ 再次获取缓存数据name userCache.get(1);System.out.println(name);cacheConfig.close();}
}5.1.2.2.3、EhCache 数据存储位置
EhCache3.x 版本中不但提供了堆内缓存 heap还提供了堆外缓存 off-heap并且还提供了数据的持久化操作可以将数据落到磁盘中 disk
heap使用堆内内存 heap(10)当前 Cache 最多只能存储 10 个数据当你 put 第 11 个数据时第一个数据就会被移除heap(10,大小单位MB)当前 Cache 最多只能存储 10MB 数据 off-heap堆外内存。将存储的数据放到操作系统的一块内存区域存储不是JVM内部这块空间属于RAM。这种对象是不能直接拿到JVM中使用的在存储时需要对数据进行序列化操作同时获取出来的时候也要做反序列化操作disk磁盘。将数据落到本地磁盘这样的话当服务重启后依然会从磁盘反序列化数据到内存中
EhCache 提供了三种组合方式
heap off-heapheap diskheap off-heap disk 在组合情况下存储存储数据时数据先落到堆内内存同时同步到对外内存以及本地磁盘。本地底盘因为空间充裕所以本地磁盘数据是最全的。而且 EhCache 要求空间大小必须 disk off-heap heap
在组合情况下读取因为性能原型肯定是先找heap查询数据没有数据去off-heap查询数据off-heap没有数据再去disk中读取数据同时读取数据之后可以将数据一次同步到off-heap、heap
通过 API 实现组合存储方式
public EhCacheConfig() {String path D:/temp;this.cacheManager CacheManagerBuilder.newCacheManagerBuilder()// 设置disk存储的位置.with(CacheManagerBuilder.persistence(path)).withCache(userCache,CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class,String.class,ResourcePoolsBuilder.newResourcePoolsBuilder()// 堆内存 最大条目数.heap(100)// 堆外内存.offheap(10, MemoryUnit.MB)// 磁盘 记得添加true才能正常的持久化并且序列化以及反序列化.disk(100, MemoryUnit.MB, true).build()).build())// 初始化 CacheManager.build(true);
}本地磁盘存储的方式一共有三个文件
mata元数据存储记录这当前 cache 的 key 类型和 value 类型data存储具体数据的位置将数据序列化成字节存储index类似索引帮助查看数据的
5.1.2.2.4、数据生存时间
因为数据如果一致存放在内存当中可能会出现内存泄漏等问题数据在内存一致不用还占着空间
EhCache 对数据设置生存时间的机制提供了三种机制【三选一】
noExpiration不设置生存时间timeToLiveExpiration从数据落到缓存计算生存时间timeToIdleExpiration从最后一个 get 计算生存时间
public EhCacheConfig() {String path D:/temp;this.cacheManager CacheManagerBuilder.newCacheManagerBuilder()// 设置disk存储的位置.with(CacheManagerBuilder.persistence(path)).withCache(userCache,CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class,String.class,ResourcePoolsBuilder.newResourcePoolsBuilder()// 堆内存 最大条目数.heap(100)// 堆外内存.offheap(10, MemoryUnit.MB)// 磁盘 记得添加true才能正常的持久化并且序列化以及反序列化.disk(100, MemoryUnit.MB, true).build())// 【三选一】不设置生存时间//.withExpiry(ExpiryPolicy.NO_EXPIRY)// 设置生存时间从存储开始计算//.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMillis(1000)))// 设置生存时间每次获取数据后重置生存时间.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(1000))).build())// 初始化 CacheManager.build(true);
}5.2、在 SpringBoot 中使用 Ehcache
5.2.1、引入依赖
dependencygroupIdorg.ehcache/groupIdartifactIdehcache/artifactIdversion3.9.3/version
/dependency5.2.2、配置 Ehcache
5.2.2.1、XML 配置文件
5.2.2.1.1、新建一个配置文件
config xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlnshttp://www.ehcache.org/v3xsi:schemaLocationhttp://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsdcache aliasuserCachekey-typejava.lang.Integer/key-typevalue-typejava.lang.String/value-typeexpiry!– 设置过期时间为 5 分钟 –ttl unitminutes5/ttl/expiryresources!– 最大条目数 –heap unitentries100/heap/resources/cache
/config5.2.2.1.2、启动类开启缓存注解
启动类上添加注解 EnableCaching
5.2.2.1.3、添加配置类
Configuration
public class EhCacheConfig {Bean(name ehCacheManager)public CacheManager cacheManager() {XmlConfiguration xmlConfig new XmlConfiguration(EhCacheConfig.class.getResource(/ehcache/ehcache.xml));CacheManager cacheManager CacheManagerBuilder.newCacheManager(xmlConfig);// 确保初始化cacheManager.init();return cacheManager;}
}5.2.2.1.4、使用 Ehcache
RestController
public class TeController {Autowiredprivate CacheManager cacheManager;GetMapping(/test)public String test(Integer key) {CacheInteger, String userCache cacheManager.getCache(userCache, Integer.class, String.class);userCache.put(key, Value for key);String value userCache.get(key);System.out.println(value);userCache.remove(key);System.out.println(删除成功);value userCache.get(key);System.out.println(value);return Value for key;}
}5.2.2.2、API 配置
5.2.2.2.1、新建一个配置类
Configuration
public class EhCacheConfig {Bean(name ehCacheManager)public CacheManager cacheManager() {String path D:/temp;return CacheManagerBuilder.newCacheManagerBuilder()// 设置disk存储的位置.with(CacheManagerBuilder.persistence(path)).withCache(userCache,CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class,String.class,ResourcePoolsBuilder.newResourcePoolsBuilder()// 堆内存 最大条目数.heap(100)// 堆外内存.offheap(10, MemoryUnit.MB)// 磁盘 记得添加true才能正常的持久化并且序列化以及反序列化.disk(100, MemoryUnit.MB, true).build())// 设置生存时间每次获取数据后重置生存时间.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(1000))).build())// 初始化 CacheManager.build(true);}}5.2.2.2.2、使用 Ehcache
RestController
public class TeController {Autowiredprivate CacheManager cacheManager;GetMapping(/test)public String test(Integer key) {CacheInteger, String userCache cacheManager.getCache(userCache, Integer.class, String.class);userCache.put(key, Value for key);String value userCache.get(key);System.out.println(value);userCache.remove(key);System.out.println(删除成功);value userCache.get(key);System.out.println(value);return Value for key;}
}5.3、集成 Spring Cache
5.3.1、引入依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId
/dependency
dependencygroupIdorg.ehcache/groupIdartifactIdehcache/artifactIdversion3.9.3/version
/dependency5.3.2、启动类开启缓存注解
启动类上添加注解 EnableCaching
5.3.3、配置 Ehcache
这里以 API 的方式进行配置并将配置信息放入 yml 配置文件中
5.3.3.1、添加配置信息
yml 配置文件中添加如下信息
准备EhCache基础配置项
ehcache:heap: 1000 # 堆内内存缓存个数off-heap: 10 # 对外内存存储大小 MBdisk: 20 # 磁盘存储数据大小 MBdiskDir: D:/data/ # 磁盘存储路径cacheNames: # 基于CacheManager构建多少个缓存- userCache- itemCache5.3.3.2、配置属性类 Data Component ConfigurationProperties(prefix ehcache) public class EhCacheProperty {private int heap;private int offheap;private int disk;private String diskDir;private SetString cacheNames; }5.3.3.3、配置 CachaManager Configuration public class EhCacheConfig {Autowiredprivate EhCacheProperty ehCacheProperty;Bean(name ehCacheManager)public CacheManager cacheManager() {// ①设置内存存储位置和数量大小ResourcePools resourcePools ResourcePoolsBuilder.newResourcePoolsBuilder()// 堆内存.heap(ehCacheProperty.getHeap())// 堆外内存.offheap(ehCacheProperty.getOffheap(), MemoryUnit.MB)// 磁盘.disk(ehCacheProperty.getDisk(),MemoryUnit.MB, true).build();// ②设置生存时间ExpiryPolicy userExpiry ExpiryPolicyBuilder.noExpiration();ExpiryPolicy itemExpiry ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(1000));// ③设置 CacheConfigurationCacheConfiguration userCache CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, resourcePools).withExpiry(userExpiry).build();CacheConfiguration itemCache CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, resourcePools).withExpiry(itemExpiry).build();// ④设置磁盘存储的位置CacheManagerBuilderPersistentCacheManager cacheManagerBuilder CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(ehCacheProperty.getDiskDir()));// ⑤设置缓存名称SetString cacheNames ehCacheProperty.getCacheNames();MapString, CacheConfiguration cacheMap new HashMap(2);cacheMap.put(userCache, userCache);cacheMap.put(itemCache, itemCache);for (String cacheName : cacheNames) {cacheManagerBuilder cacheManagerBuilder.withCache(cacheName, cacheMap.get(cacheName));}// 初始化 CacheManagerreturn cacheManagerBuilder.build(true);}}5.3.4、使用 Ehcache Spring Boot 提供了 Cacheable、CachePut、CacheEvict 等注解来简化缓存操作同时也支持基于缓存管理器的编程方式 5.3.4.1、基于注解的缓存 5.3.4.1.1、新建一个 UserServiceImpl Slf4j Service CacheConfig(cacheNames userCache) public class UserServiceImpl {// 模拟数据库数据private MapInteger, User userMap new HashMap();CachePut(key #user.id)public User add(User user) {log.info(add);userMap.put(user.getId(), user);return user;}Cacheable(key #id, unless #result null)public User get(Integer id) {log.info(get);return userMap.get(id);}CachePut(key #user.id)public User update(User user) {log.info(update);userMap.put(user.getId(), user);return user;}CacheEvict(key #id)public void delete(Integer id) {log.info(delete);userMap.remove(id);}}5.3.4.1.2、新建一个 TestController RestController public class TeController {Autowiredprivate UserServiceImpl userServiceImpl;PostMappingpublic String add(RequestBody User user) {userServiceImpl.add(user);return add;}GetMapping(/{id})public User get(PathVariable Integer id) {User user userServiceImpl.get(id);return user;}PutMappingpublic String update(RequestBody User user) {userServiceImpl.update(user);return update;}DeleteMapping(/{id})public String delete(PathVariable Integer id) {userServiceImpl.delete(id);return delete;}}5.3.4.12、基于缓存管理器的编程方式 Spring Boot 中可以使用 CacheManager 和 Cache 接口来实现缓存的管理和操作 RestController public class TeController {Autowiredprivate CacheManager cacheManager;PostMappingpublic String add(RequestBody User user) {CacheInteger, String userCache cacheManager.getCache(userCache, Integer.class, String.class);if (Objects.nonNull(userCache)) {userCache.put(user.getId(), JSON.toJSONString(user));}return add;}GetMapping(/{id})public User get(PathVariable Integer id) {CacheInteger, String userCache cacheManager.getCache(userCache, Integer.class, String.class);if (Objects.nonNull(userCache)) {String s userCache.get(id);return JSON.parseObject(s, User.class);}return null;}PutMappingpublic String update(RequestBody User user) {CacheInteger, String userCache cacheManager.getCache(userCache, Integer.class, String.class);if (Objects.nonNull(userCache)) {userCache.put(user.getId(), JSON.toJSONString(user));}return update;}DeleteMapping(/{id})public String delete(PathVariable Integer id) {CacheInteger, String userCache cacheManager.getCache(userCache, Integer.class, String.class);if (Objects.nonNull(userCache)) {userCache.remove(id);}return delete;} }
- 上一篇: wordpress建站技巧电话销售电销系统
- 下一篇: WordPress建站可以吗免费的网站加速器
相关文章
-
wordpress建站技巧电话销售电销系统
wordpress建站技巧电话销售电销系统
- 站长
- 2026年02月18日
-
wordpress建站公司上海市房地产官网
wordpress建站公司上海市房地产官网
- 站长
- 2026年02月18日
-
wordpress建站方法品牌展柜设计制作
wordpress建站方法品牌展柜设计制作
- 站长
- 2026年02月18日
-
WordPress建站可以吗免费的网站加速器
WordPress建站可以吗免费的网站加速器
- 站长
- 2026年02月18日
-
wordpress建站流量网页设计与制作项目教程黑马程序员
wordpress建站流量网页设计与制作项目教程黑马程序员
- 站长
- 2026年02月18日
-
wordpress建站哪里好ui培训学费一般多少
wordpress建站哪里好ui培训学费一般多少
- 站长
- 2026年02月18日
