【开源项目系列】如何基于 Spring Cache 实现多级缓存(同时整合本地缓存 Ehcache 和分布式缓存 Redis) (2)

配置 Ehcache:

# Ehcache Config ## the path of ehcache.xml (We can put it directly under Resources) h2cache.ehcache.filePath=ehcache.xml #Set whether the EhCache CacheManager should be shared (as a singleton at the ClassLoader level) or independent (typically local within the application).Default is "false", creating an independent local instance. h2cache.ehcache.shared=true

配置 Redis:主要包括默认的缓存配置和自定义缓存配置

要注意一点的是:h2cache-spring-boot-starter 同时引入了 Lettuce 和 Jedis 客户端,而 Spring Boot 默认使用 Lettuce 客户端,所以如果我们需要使用 Jedis 客户端,需要将 Lettuce 依赖去除掉。

# Redis Config ## default Config (expire) h2cache.redis.default-config.ttl=200 ### Disable caching {@literal null} values.Default is "false" h2cache.redis.default-config.disable-null-values=true ### Disable using cache key prefixes.Default is "true" h2cache.redis.default-config.use-prefix=true ## Custom Config list ### cacheName -> @CacheConfig#cacheNames @Cacheable#cacheNames and other comments, etc h2cache.redis.config-list[0].cache-name=userCache h2cache.redis.config-list[0].ttl=60 h2cache.redis.config-list[0].use-prefix=true h2cache.redis.config-list[0].disable-null-values=true h2cache.redis.config-list[1].cache-name=bookCache h2cache.redis.config-list[1].ttl=60 h2cache.redis.config-list[1].use-prefix=true #Redis spring.redis.host=10.111.0.111 spring.redis.password= spring.redis.port=6379 spring.redis.database=15 # 连接池最大连接数(使用负值表示没有限制) spring.redis.jedis.pool.max-active=8 # 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0 # 连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=30 如何使用缓存注解

我们只要像之前一样使用 Spring Cache 的注解即可。

for example:

代码里的持久层,我使用的是: .

package com.hyf.testDemo.redis; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Caching; import org.springframework.stereotype.Repository; /** * @author Howinfun * @desc * @date 2020/3/25 */ @Repository // Global cache config,We usually set the cacheName @CacheConfig(cacheNames = {"userCache"}) public interface UserMapper extends BaseMapper<User> { /** * put the data to cache(Ehcache & Redis) * @param id * @return */ @Cacheable(key = "#id",unless = "#result == null") User selectById(Long id); /** * put the data to cache After method execution * @param user * @return */ @CachePut(key = "#user.id", condition = "#user.name != null and #user.name != \'\'") default User insert0(User user) { this.insert(user); return user; } /** * evict the data from cache * @param id * @return */ @CacheEvict(key = "#id") int deleteById(Long id); /** * Using cache annotations in combination * @param user * @return */ @Caching( evict = {@CacheEvict(key = "#user.id", beforeInvocation = true)}, put = {@CachePut(key = "#user.id")} ) default User updateUser0(User user){ this.updateById(user); return user; } } 测试一下:

查询:我们可以看到,在数据库查询到结果后,会将数据添加到 Ehcache 和 Redis 缓存中;接着之后的查询都将会先从 Ehcache 或者 Redis 里查询。

2020-04-03 09:55:09.691 INFO 5920 --- [nio-8080-exec-7] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2020-04-03 09:55:10.044 INFO 5920 --- [nio-8080-exec-7] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2020-04-03 09:55:10.051 DEBUG 5920 --- [nio-8080-exec-7] c.h.t.redis.BookMapper2.selectById : ==> Preparing: SELECT id,create_time,update_time,read_frequency,version,book_name FROM book WHERE id=? 2020-04-03 09:55:10.068 DEBUG 5920 --- [nio-8080-exec-7] c.h.t.redis.BookMapper2.selectById : ==> Parameters: 51(Long) 2020-04-03 09:55:10.107 DEBUG 5920 --- [nio-8080-exec-7] c.h.t.redis.BookMapper2.selectById : <== Total: 1 2020-04-03 09:55:10.113 INFO 5920 --- [nio-8080-exec-7] c.hyf.cache.cachetemplate.H2CacheCache : insert into ehcache,key:51,value:Book2(id=51, bookName=微服务架构, readFrequency=1, createTime=2020-03-20T16:10:13, updateTime=2020-03-27T09:14:44, version=1) 2020-04-03 09:55:10.118 INFO 5920 --- [nio-8080-exec-7] c.hyf.cache.cachetemplate.H2CacheCache : insert into redis,key:51,value:Book2(id=51, bookName=微服务架构, readFrequency=1, createTime=2020-03-20T16:10:13, updateTime=2020-03-27T09:14:44, version=1) 2020-04-03 09:55:31.864 INFO 5920 --- [nio-8080-exec-2] c.hyf.cache.cachetemplate.H2CacheCache : select from ehcache,key:51

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwdppw.html