spring-boot集成redis实现缓存功能

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 一、本次案例 我们假设用户信息(user)和产品信息(product)需要缓存,缓存数据都加上user和product作为key前缀,采用用户的id和产品的id作为key的后缀。用户缓存时间为30分钟,产品信息缓存时间为10分钟。

一、本次案例

我们假设用户信息(user)和产品信息(product)需要缓存,缓存数据都加上user和product作为key前缀,采用用户的id和产品的id作为key的后缀。用户缓存时间为30分钟,产品信息缓存时间为10分钟。

二、redis客户端配置

无论使用spring-boot的哪个版本,我们都需要先配置redis连接,两个版本的redis客户端连接池使用有所不同。

spring-boot版本 默认客户端类型
1.5.x jedis
2.x lettuce

在1.5.x中,我们配置jedis连接池只需要配置 spring.redis.pool.* 开始的配置即可,如下配置

spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
spring.redis.pool.max-idle=8

但在2.x版本中由于引入了不同的客户端,需要指定配置哪种连接池,如下配置

#jedis客户端
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-idle=8
#lettuce客户端
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.shutdown-timeout=100ms

除此之外还可以看到时间配置上还需要带上对应的单位。

三、直接配置cache参数

在1.5.x版本下提供的针对cache的配置非常少,我们能够使用到的自动装配redis缓存的配置如下

#缓存的名称集合,多个采用逗号分割
spring.cache.cache-names=
#缓存的类型,官方提供了很多,这里我们填写redis
spring.cache.type=

在2.x版本时,官方增加了更多的配置

#缓存的名称集合,多个采用逗号分割
spring.cache.cache-names=
#缓存的类型,官方提供了很多,这里我们填写redis
spring.cache.type=
#是否缓存null数据,默认是false
spring.cache.redis.cache-null-values=
#redis中缓存超时的时间,默认60000ms
spring.cache.redis.time-to-live=
#缓存数据key是否使用前缀,默认是true
spring.cache.redis.use-key-prefix=
#缓存数据key的前缀,在上面的配置为true时有效,
spring.cache.redis.key-prefix=

四、JavaConfig方式配置

通用配置方式只能满足整个程序所有缓存都采用相同公共配置的方式,如果需要特殊处理,如我们的案列,则需要自己采用代码的方式来配置。
采用代码的方式,只要需要配置的是CacheMananger,采用Redis时具体实现我们需要使用其子类RedisCacheMananger来做配置

4.1、spring-boot 1.5.x版本

CacheManager配置

@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
    //默认超时时间,单位秒
    redisCacheManager.setDefaultExpiration(60);
    //缓存超时时间Map,key为cacheName,value为超时,单位是秒
    Map<String, Long> expiresMap = new HashMap<>();
    //缓存用户信息的cacheName和超时时间
    expiresMap.put("user", 1800L);
    //缓存产品信息的cacheName和超时时间
    expiresMap.put("product", 600L);
    redisCacheManager.setExpires(expiresMap);
    return redisCacheManager;
}

cache调用代码

@Cacheable(value = "user", key = "'user:'+#id", unless = "#result==null")
public String getUser(int id) {
    //逻辑操作
}
@Cacheable(value = "product", key = "'product:'+#id", unless = "#result==null")
public String getProduct(int id) {
    //逻辑操作
}

4.2、spring-boot 2.x版本

CacheManager配置

2.x版本开始,代码方式配置变化比较大,同时增加了更多配置参数

@Bean
CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
    //user信息缓存配置
    RedisCacheConfiguration userCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(30)).disableCachingNullValues().prefixKeysWith("user");
    //product信息缓存配置
    RedisCacheConfiguration productCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10)).disableCachingNullValues().prefixKeysWith("product");
    Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
    redisCacheConfigurationMap.put("user", userCacheConfiguration);
    redisCacheConfigurationMap.put("product", productCacheConfiguration);
    //初始化一个RedisCacheWriter
    RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
    
    
    //设置CacheManager的值序列化方式为JdkSerializationRedisSerializer,但其实RedisCacheConfiguration默认就是使用StringRedisSerializer序列化key,JdkSerializationRedisSerializer序列化value,所以以下注释代码为默认实现
    //ClassLoader loader = this.getClass().getClassLoader();
    //JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(loader);
    //RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jdkSerializer);
    //RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);
    
    
    RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
    //设置默认超过期时间是30秒
    defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
    //初始化RedisCacheManager
    RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap);
    return cacheManager;
}

以上代码中RedisCacheConfiguration类为2.x新增的配置类,增加了几个配置项。这里比较奇怪的是调用它的配置方法每一次都会重新生成一个配置对象,而不是在原对象上直接修改参数值,这一点本人没搞懂作者为何要选择这种方式。

cache调用代码

@Cacheable(value = "user", key = "#id", unless = "#result==null")
public String getUser(int id) {
    //逻辑操作
}
@Cacheable(value = "product", key = "#id", unless = "#result==null")
public String getProduct(int id) {
    //逻辑操作
}

这里两点需要注意的地方

  • 1、在Cacheable的key属性中不在需要自己配置前缀,可以在RedisCacheConfiguration中配置。
  • 2、如果Cacheable中不配置 unless = "#result==null" 属性,而RedisCacheConfiguration中调用disableCachingNullValues()配置了不缓存null结果在出现null结果时会报异常,这一点与我一开始的理解也不太一样,二者并非都生效的。个人理解unless配置#result==null可以决定null的值是否往cacheManager丢,而RedisCacheConfiguration的disableCachingNullValues()只在存储前起到了校验作用,而不会因为设置了这个值而直接跳过null的保存。如果真是设计如此,感觉有点鸡肋,不知道算不算一个bug。

小结

spring-boot刚刚发布了2.0.0的正式版,除了比较大的webflux之外,许多组件可能都有细节变化,在做升级的时候需要注意更改,建议先验证常用的功能用法后,再做升级的计划安排。

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
1天前
|
缓存 NoSQL 关系型数据库
【Redis】Redis 缓存重点解析
【Redis】Redis 缓存重点解析
12 0
|
2天前
|
缓存 NoSQL 关系型数据库
【Redis】Redis作为缓存
【Redis】Redis作为缓存
6 0
|
2天前
|
存储 缓存 监控
利用Redis构建高性能的缓存系统
在现今高负载、高并发的互联网应用中,缓存系统的重要性不言而喻。Redis,作为一款开源的、内存中的数据结构存储系统,它可以用作数据库、缓存和消息代理。本文将深入探讨Redis的核心特性,以及如何利用Redis构建高性能的缓存系统,并通过实际案例展示Redis在提升系统性能方面的巨大潜力。
|
2天前
|
存储 缓存 NoSQL
【技术分享】求取列表需求的redis缓存方案
【技术分享】求取列表需求的redis缓存方案
14 0
|
2天前
|
负载均衡 监控 NoSQL
Redis的几种主要集群方案
【5月更文挑战第15天】Redis集群方案包括主从复制(基础,读写分离,手动故障恢复)、哨兵模式(自动高可用,自动故障转移)和Redis Cluster(官方分布式解决方案,自动分片、容错和扩展)。此外,还有Codis、Redisson和Twemproxy等工具用于代理分片和负载均衡。选择方案需考虑应用场景、数据量和并发需求,权衡可用性、性能和扩展性。
26 2
|
2天前
|
存储 监控 负载均衡
保证Redis的高可用性是一个涉及多个层面的任务,主要包括数据持久化、复制与故障转移、集群化部署等方面
【5月更文挑战第15天】保证Redis高可用性涉及数据持久化、复制与故障转移、集群化及优化策略。RDB和AOF是数据持久化方法,哨兵模式确保故障自动恢复。Redis Cluster实现分布式部署,提高负载均衡和容错性。其他措施包括身份认证、多线程、数据压缩和监控报警,以增强安全性和稳定性。通过综合配置与监控,可确保Redis服务的高效、可靠运行。
22 2
|
2天前
|
存储 NoSQL Redis
Redis源码、面试指南(5)多机数据库、复制、哨兵、集群(下)
Redis源码、面试指南(5)多机数据库、复制、哨兵、集群
18 1
|
2天前
|
监控 NoSQL Redis
Redis源码、面试指南(5)多机数据库、复制、哨兵、集群(上)
Redis源码、面试指南(5)多机数据库、复制、哨兵、集群
32 0
|
2天前
|
存储 监控 NoSQL
Redis哨兵&分片集群
Redis哨兵&分片集群
20 0

热门文章

最新文章