暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

你知道如何在springboot中使用redis吗

FleyX学习笔记 2019-05-29
225


说明:本文针对的是新版 spring boot 2.1.3,其 spring data 依赖为 spring-boot-starter-data-redis,且其默认连接池为 lettuce

  redis 作为一个高性能的内存数据库,如果不会用就太落伍了,之前在 node.js 中用过 redis,本篇记录如何将 redis 集成到 spring boot 中。提供 redis 操作类,和注解使用 redis 两种方式。主要内容如下:

  • docker 安装 redis

  • springboot 集成 redis

  • 编写 redis 操作类

  • 通过注解使用 redis

安装 redis

  通过 docker 安装,docker compose 编排文件如下:

  1. # docker-compose.yml

  2. version: "2"

  3. services:

  4. redis:

  5. container_name: redis

  6. image: redis:3.2.10

  7. ports:

  8. - "6379:6379"

  然后在 docker-compose.yml
所在目录使用 docker-compose up-d
命令,启动 redis。

集成 springboot

  说明:springboot 版本为 2.1.3

添加 maven 依赖

  只需添加 spring-boot-starter-data-redis
依赖即可,并排除 lettuce 依赖,然后引入 jedis 和 jedis 的依赖 commons-pool2

  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-data-redis</artifactId>

  4. <exclusions>

  5. <exclusion>

  6. <groupId>io.lettuce</groupId>

  7. <artifactId>lettuce-core</artifactId>

  8. </exclusion>

  9. </exclusions>

  10. </dependency>


  11. <dependency>

  12. <groupId>org.apache.commons</groupId>

  13. <artifactId>commons-pool2</artifactId>

  14. </dependency>


  15. <dependency>

  16. <groupId>redis.clients</groupId>

  17. <artifactId>jedis</artifactId>

  18. </dependency>

编写 springboot 配置文件

  配置文件如下:

  1. server:

  2. port: 8081

  3. servlet:

  4. context-path: /sso

  5. spring:

  6. application:

  7. name: SSO

  8. cache:

  9. type: redis

  10. redis:

  11. database: 0

  12. host: 192.168.226.5

  13. port: 6379

  14. # 有密码填密码,没有密码不填

  15. password:

  16. # 连接超时时间(ms)

  17. timeout: 1000ms

  18. # 高版本springboot中使用jedis或者lettuce

  19. jedis:

  20. pool:

  21. # 连接池最大连接数(负值表示无限制)

  22. max-active: 8

  23. # 连接池最大阻塞等待时间(负值无限制)

  24. max-wait: 5000ms

  25. # 最大空闲链接数

  26. max-idle: 8

  27. # 最小空闲链接数

  28. min-idle: 0

编写配置类

  配置类代码如下:

  1. @EnableCaching//开启缓存

  2. @Configuration

  3. public class RedisConfig extends CachingConfigurerSupport {


  4. /**

  5. * 设置缓存管理器,这里可以配置默认过期时间等

  6. *

  7. * @param connectionFactory 连接池

  8. * @return

  9. */

  10. @Bean

  11. public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {

  12. RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration

  13. .defaultCacheConfig()

  14. .entryTtl(Duration.ofSeconds(60));

  15. //注意:请勿使用先new 配置对象,然后在调用entryTtl方法的方式来操作

  16. //会导致配置不生效,原因是调用.entryTtl方法会返回一个新的配置对象,而不是在原来的配置对象上修改


  17. RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);

  18. RedisCacheManager manager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);

  19. return manager;

  20. }


  21. @SuppressWarnings("all")

  22. @Bean

  23. public RedisTemplate<String, String> redisTemplate(JedisConnectionFactory factory) {

  24. StringRedisTemplate template = new StringRedisTemplate(factory);

  25. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

  26. ObjectMapper om = new ObjectMapper();

  27. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

  28. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

  29. jackson2JsonRedisSerializer.setObjectMapper(om);

  30. RedisSerializer stringSerializer = new StringRedisSerializer();

  31. template.setKeySerializer(stringSerializer);

  32. template.setValueSerializer(jackson2JsonRedisSerializer);

  33. template.setHashKeySerializer(stringSerializer);

  34. template.setHashValueSerializer(jackson2JsonRedisSerializer);

  35. template.afterPropertiesSet();

  36. return template;

  37. }


  38. //使用jedis连接池建立jedis连接工厂

  39. @Bean

  40. public JedisConnectionFactory jedisConnectionFactory() {

  41. logger.info("jedisConnectionFactory:初始化了");

  42. JedisPoolConfig config = new JedisPoolConfig();

  43. config.setMaxIdle(maxIdle);

  44. config.setMinIdle(minIdle);

  45. config.setMaxWaitMillis(maxWaitMillis);

  46. config.setMaxTotal(maxActive);

  47. //链接耗尽时是否阻塞,默认true

  48. config.setBlockWhenExhausted(true);

  49. //是否启用pool的jmx管理功能,默认true

  50. config.setJmxEnabled(true);

  51. JedisConnectionFactory factory = new JedisConnectionFactory();

  52. factory.setPoolConfig(config);

  53. factory.setHostName(host);

  54. factory.setPort(port);

  55. factory.setPassword(password);

  56. factory.setDatabase(database);

  57. factory.setTimeout(timeout);

  58. return factory;

  59. }

  60. }

使用方法

  有两种方法来进行缓存操作,一种是在方法上添加缓存注解实现各种操作,一种是手动控制。个人比较喜欢手动控制,觉得这样都在自己的掌控中。

通过注解使用

  主要有以下 5 个注解:

  • @CacheConfig
    : 类级别缓存,设置缓存 key 前缀之类的

  • @Cacheable
    : 触发缓存入口

  • @CacheEvict
    : 触发移除缓存

  • @CachePut
    : 更新缓存

  • @Caching
    : 组合缓存

@CacheConfig

  该注解可以将缓存分类,它是类级别注解,主要用于给某个类的缓存全局配置,例子如下:

  1. @CacheConfig(cacheNames = "redis_test")

  2. @Service

  3. public class RedisService {

  4. //....

  5. }

上面 CacheConfig 会给类下通过注解生成的 key 加上 redis_test 的前缀。

@Cacheable

  方法级别注解,根据 key 查询缓存:

  • 如果 key 不存在,将方法返回值缓存到 redis 中

  • 如果 key 存在,直接从缓存中取值 例子如下:

  1. /**

  2. * 缓存时间,首次查询后会缓存结果,key中的值可使用表达式计算.

  3. * 如不提供key,将使用默认key构造方法生成一个key

  4. * @return long

  5. */

  6. @Cacheable(key = "'currentTime'")

  7. public long getTime() {

  8. return System.currentTimeMillis();

  9. }

多次调用此段代码会发现每次返回的值都是一样的。

CachePut

  用于更新缓存,每次调用都会想 db 请求,缓存数据

  • 如果 key 存在,更新内容

  • 如果 key 不存在,插入内容

代码如下:

  1. /**

  2. * 一般用于更新查插入操作,每次都会请求db

  3. */

  4. @CachePut(key = "'currentTime'+#id")

  5. public long updateTime(String id) {

  6. return System.currentTimeMillis();

  7. }

每次调用此方法都会根据 key 刷新 redis 中的缓存数据。

@CacheEvict

  根据 key 删除缓存中的数据。allEntries=true 表示删除缓存中所有数据。代码如下:

  1. @CacheEvict(key = "'currentTime'+#id",allEntries=false)

  2. public void deleteTime(String id) {

  3. }

@Caching

  本注解可将其他注解组合起来使用。比如下面的例子:

  1. //value属性为key指定前缀

  2. @Caching(put = {@CachePut(value = "user", key = "'name_'+#user.name"),

  3. @CachePut(value = "user", key = "'pass_'+#user.password")})

  4. public User testCaching(User user) {

  5. return user;

  6. }

上面的代码执行后将在 redis 中插入两条记录。使用 keys*
将看到如下结果:

手动控制

  手动控制就相当于 mybatis 的手写 sql 语句,需要调用 redisTemplate
中的各种方法来进行缓存查询,缓存更新,缓存删除等操作。

  使用方法参见 util/RedisUtil 中的方法。redisTemplate
基本可以实现所有的 redis 操作。

本篇原创发布于:https://www.tapme.top/blog/detail/2019-02-22-14-59

项目源码::https://github.com/FleyX/demo-project/tree/master/1.SSO%E5%8D%95%E7%82%B9%E7%99%BB%E5%BD%95/sso


文章转载自FleyX学习笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论