自己做网站 服务器,wordpress做多语言版,哪里有免费的网站自己做,学院网站建设推进会1.什么是encache
EhCache 是一个纯 Java 的进程内缓存框架#xff0c;具有快速、精干等特点#xff0c;是 Hibernate 中默认的 CacheProvider。 Ehcache 特性
优点
快速、简单支持多种缓存策略#xff1a;LRU、LFU、FIFO 淘汰算法缓存数据有两级#xff1a;内存和磁盘具有快速、精干等特点是 Hibernate 中默认的 CacheProvider。 Ehcache 特性
优点
快速、简单支持多种缓存策略LRU、LFU、FIFO 淘汰算法缓存数据有两级内存和磁盘因此无需担心容量问题缓存数据会在虚拟机重启的过程中写入磁盘可以通过 RMI、可插入 API 等方式进行分布式缓存具有缓存和缓存管理器的侦听接口支持多缓存管理器实例以及一个实例的多个缓存区域提供 Hibernate 的缓存实现
缺点
使用磁盘 Cache 的时候非常占用磁盘空间不保证数据的安全虽然支持分布式缓存但效率不高通过组播方式在不同节点之间同步数据。
2.代码工程
实验目标
利用encache实现service层缓存功能 pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringboot-demo/artifactIdgroupIdcom.et/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdehcache/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-autoconfigure/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependencydependencygroupIdnet.sf.ehcache/groupIdartifactIdehcache/artifactId/dependency/dependencies/project
controller
package com.et.controller;import com.et.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;RestController
public class HelloWorldController {AutowiredHelloService helloService;RequestMapping(/hello)public MapString, Object showHelloWorld(String name){MapString, Object map new HashMap();map.put(msg, helloService.sayhi(name));return map;}
}
service
package com.et.service;import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;/*** ClassName Hello* Description TODO* Author liuhaihua* Date 2024/10/4 20:24* Version 1.0*/
Service
public class HelloService {Cacheable(value customcache)public String sayhi(String name){System.out.println(name:name);return hi,name;}
}
DemoApplication.java
Cacheable
表明所修饰的方法是可以缓存的当第一次调用这个方法时它的结果会被缓存下来在缓存的有效时间内以后访问这个方法都直接返回缓存结果不再执行方法中的代码段。 这个注解可以用condition属性来设置条件如果不满足条件就不使用缓存能力直接执行方法。 可以使用key属性来指定 key 的生成规则。
CachePut
与Cacheable不同CachePut不仅会缓存方法的结果还会执行方法的代码段。 它支持的属性和用法都与Cacheable一致。
CacheEvict
与Cacheable功能相反CacheEvict表明所修饰的方法是用来删除失效或无用的缓存数据。 下面是Cacheable、CacheEvict和CachePut基本使用方法的一个集中展示
package com.et;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;SpringBootApplication
EnableCaching
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
application.yaml
springboot在启动的时候可以对ehcache.xml配置文件进行解析拿到缓存策略交给ehcache去缓存处理。
server:port: 8088spring:cache:ehcache:config: classpath:config/encache.xml
encache.xml
Ehcache 的CacheManager构造函数或工厂方法被调用时会默认加载 classpath 下名为ehcache.xml的配置文件。如果加载失败会加载 Ehcache jar 包中的ehcache-failsafe.xml文件这个文件中含有简单的默认配置。 ehcache.xml 配置参数说明
name缓存名称。maxElementsInMemory缓存最大个数。eternal缓存中对象是否为永久的如果是超时设置将被忽略对象从不过期。timeToIdleSeconds置对象在失效前的允许闲置时间单位秒。仅当 eternalfalse 对象不是永久有效时使用可选属性默认值是 0也就是可闲置时间无穷大。timeToLiveSeconds缓存数据的生存时间TTL也就是一个元素从构建到消亡的最大时间间隔值这只能在元素不是永久驻留时有效如果该值是 0 就意味着元素可以停顿无穷长的时间。maxEntriesLocalDisk当内存中对象数量达到 maxElementsInMemory 时Ehcache 将会对象写到磁盘中。overflowToDisk内存不足时是否启用磁盘缓存。diskSpoolBufferSizeMB这个参数设置 DiskStore磁盘缓存的缓存区大小。默认是 30MB。每个 Cache 都应该有自己的一个缓冲区。maxElementsOnDisk硬盘最大缓存个数。diskPersistent是否在 VM 重启时存储硬盘的缓存数据。默认值是 false。diskExpiryThreadIntervalSeconds磁盘失效线程运行时间间隔默认是 120 秒。memoryStoreEvictionPolicy当达到 maxElementsInMemory 限制时Ehcache 将会根据指定的策略去清理内存。默认策略是 LRU最近最少使用。你可以设置为 FIFO先进先出或是 LFU较少使用。clearOnFlush内存数量最大时是否清除。
ehcache xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:noNamespaceSchemaLocation../config/ehcache.xsddiskStore pathjava.io.tmpdir/defaultCachemaxElementsInMemory10000eternalfalsetimeToIdleSeconds120timeToLiveSeconds120maxElementsOnDisk10000000diskExpiryThreadIntervalSeconds120memoryStoreEvictionPolicyLRUpersistence strategylocalTempSwap//defaultCache!-- Custom cache strategy. The name attribute value of the custom cache strategy is users. If you define multiple cache strategies, the name values cannot be the same. --cache namecustomcachemaxElementsInMemory10000eternalfalsetimeToIdleSeconds120timeToLiveSeconds120maxElementsOnDisk10000000diskExpiryThreadIntervalSeconds120memoryStoreEvictionPolicyLRUpersistence strategylocalTempSwap//cache/ehcache
只是一些关键代码所有代码请参见下面代码仓库
代码仓库
GitHub - Harries/springboot-demo: a simple springboot demo with some components for example: redis,solr,rockmq and so on.(encache)
3.测试
启动SpringBoot应用连续二次访问http://127.0.0.1:8088/hello?namejack可以看出第一次会进去方法里面并返回name:jack第二次直接从encache拿到缓存内容而不会进去方法
4.引用
EhcacheSpring Boot集成encache快速入门Demo | Harries Blog™