购物车网站建设,wordpress 删除超文本,wordpress 修改链接地址,网站招标建设Ehcache 介绍EhCache 从 Hibernate 发展而来#xff0c;是一个纯Java的进程内缓存框架#xff0c;具有快速、精干等特点。Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存#xff0c;Java EE和轻量级容器。它具有内存和磁盘存储#xff0c;缓存加载器#x… Ehcache 介绍EhCache 从 Hibernate 发展而来是一个纯Java的进程内缓存框架具有快速、精干等特点。Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存Java EE和轻量级容器。它具有内存和磁盘存储缓存加载器缓存扩展缓存异常处理程序一个gzip缓存servlet过滤器支持REST和SOAP api等特点。主要特性快速简单多种缓存策略缓存数据有两级内存和磁盘因此无需担心容量问题缓存数据会在虚拟机重启的过程中写入磁盘可以通过RMI、可插入API等方式进行分布式缓存具有缓存和缓存管理器的侦听接口支持多缓存管理器实例以及一个实例的多个缓存区域提供Hibernate的缓存实现Show me the code在 pom.xml 文件中添加 Ehcache 依赖dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId
/dependency
dependencygroupIdnet.sf.ehcache/groupIdartifactIdehcache/artifactId
/dependency
不需要配置versionSpringBoot的根pom已经对版本号做了统一声明配置文件在配置文件 application.yaml 中配置 ehcache 的相关参数具体内容如下spring:application:name: spring-boot-bulking-ehcachecache:type: ehcacheehcache:config: classpath:/ehcache.xml
spring.cache.type 声明spring框架使用哪一种类型的缓存因为spring框架提供了多种缓存可供选择。添加 Ehcache 配置在src/main/resources 目录下创建配置文件ehcache.xml 内容如下ehcache nametestdiskStore pathjava.io.tmpdir/defaultCachemaxEntriesLocalHeap1000eternalfalsetimeToIdleSeconds300timeToLiveSeconds600overflowToDisktruememoryStoreEvictionPolicyLRU/defaultCachecache nameuserCachemaxEntriesLocalHeap200eternalfalsetimeToIdleSeconds300timeToLiveSeconds600overflowToDisktrue/cache
/ehcache
参数含义diskStore磁盘缓存位置name缓存名称maxElementsInMemory内存中最大缓存对象数maxElementsOnDisk硬盘中最大缓存对象数若是0表示无穷大eternaltrue表示对象永不过期此时会忽略timeToIdleSeconds和timeToLiveSeconds属性默认为falsetimeToIdleSeconds设定允许对象处于空闲状态的最长时间以秒为单位。当对象自从最近一次被访问后如果处于空闲状态的时间超过了timeToIdleSeconds属性值这个对象就会过期EHCache将把它从缓存中清空。只有当eternal属性为false该属性才有效。如果该属性值为0则表示对象可以无限期地处于空闲状态timeToLiveSeconds设定对象允许存在于缓存中的最长时间以秒为单位。当对象自从被存放到缓存中后如果处于缓存中的时间超过了 timeToLiveSeconds属性值这个对象就会过期EHCache将把它从缓存中清除。只有当eternal属性为false该属性才有效。如果该属性值为0则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性才有意义overflowToDisk当内存中对象数量达到maxElementsInMemory时Ehcache将会对象写到磁盘中。diskSpoolBufferSizeMB磁盘缓存区大小默认为30MB。每个Cache都应该有自己的一个缓存区。diskPersistent是否缓存虚拟机重启期数据。diskExpiryThreadIntervalSeconds磁盘失效线程运行时间间隔默认是120秒。memoryStoreEvictionPolicy当达到maxElementsInMemory限制时Ehcache将会根据指定的策略去清理内存。默认策略是LRU最近最少使用。你可以设置为FIFO先进先出或是LFU较少使用。clearOnFlush内存数量最大时是否清除。开启缓存入口启动类添加注解 EnableCachingSpringBootApplication(exclude {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})
EnableCaching // 开启缓存Spring Boot 会自动配置缓存的 CacheManager
public class StartApplication {public static void main(String[] args) {SpringApplication.run(StartApplication.class, args);}
}缓存业务使用Component
CacheConfig(cacheNames userCache)
public class UserService {Cacheable(key #id)public User getUserById(Long id) {System.out.println(缓存中无值);User user User.builder().id(id).userName(雪糕( id )).age(18).address(杭州).build();return user;}CachePut(key #user.id)public User updateUser(User user) {user.setUserName(雪糕new name);return user;}CacheEvict(key #id)public void deleteById(Long id) {System.out.println(db 删除数据id id);}
}
CacheConfig 作用于类上用来描述该类中所有方法使用的缓存名称。当然也可以不使用该注解直接在具体方法上的缓存注解里配置名称Cacheable 用于查询方法上表示将一个方法的返回值缓存起来。默认情况下缓存的 key 就是方法的参数缓存的 value 就是方法的返回值CachePut 更新操作当数据库中的数据更新后缓存中的数据也要跟着更新使用该注解可以将方法的返回值自动更新到已经存在的 key 上CacheEvict 删除操作当数据库中的数据删除后相关的缓存数据也要自动清除。除了采用 Cacheable 、CachePut 等方法注解解耦式操作缓存外我们也可以使用 CacheManager显示方式手动来操作缓存。CacheManagerSpring定义了CacheManager和Cache接口统一不同的缓存技术。其中CacheManager是Spring提供的各种缓存技术的抽象接口而Cache接口包含缓存的读、写、删等各种操作。针对不同的缓存技术需要实现不同的CacheManagerSpring预先定义了主流缓存框架的cacheManger实现类CacheManager描述SimpleCacheManager使用简单的Collection来存储缓存主要用于测试ConcurrentMapCacheManager使用ConcurrentMap作为缓存技术默认NoOpCacheManager测试用EhCacheCacheManager使用EhCache作为缓存技术以前在hibernate的时候经常用GuavaCacheManager使用google guava的GuavaCache作为缓存技术HazelcastCacheManager使用Hazelcast作为缓存技术JCacheCacheManager使用JCache标准的实现作为缓存技术如Apache Commons JCSRedisCacheManager使用Redis作为缓存技术CaffeineCacheManager使用Caffeine作为缓存技术Spring Boot 为我们预留接口扩展方便我们自动配置 EhCache、Redis、Guava、ConcurrentMap等缓存默认使用ConcurrentMapCacheManager。Spring Boot的application.yaml配置文件使用spring.cache前缀属性进行配置。本文我们使用 EhCache 缓存代码示例如下Component
public class UserCacheManager {Resourceprivate CacheManager cacheManager;public User getUserById(Long id) {Cache cache cacheManager.getCache(userCache);User user cache.get(id, User.class);if (user null) {System.out.println(缓存中无值);user User.builder().id(id).userName(雪糕( id )).age(18).address(杭州).build();cache.put(id, user);}return user;}public User updateUser(User user) {user.setUserName(雪糕new name);Cache cache cacheManager.getCache(userCache);cache.put(user.getId(), user);return user;}public void deleteById(Long id) {Cache cache cacheManager.getCache(userCache);cache.evict(id);System.out.println(db 删除数据id id);}
}
演示代码地址https://github.com/aalansehaiyang/spring-boot-bulking 模块spring-boot-bulking-ehcache往期推荐
Spring为什么建议构造器注入超级详细的Spring Boot 注解总结Spring中的重试功能嗯有点东西