参考视频:https://www.bilibili.com/video/BV1844y1z7LQ?spm_id_from=333.337.search-card.all.click
下载地址:https://github.com/tporadowski/redis/releases
菜鸟教程:https://www.runoob.com/redis/redis-install.html

一、引入依赖

  1. <!-- Redis数据库 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>

还需要引入通用池

参考:java-commons-pool2—(1)—连接池详解

  1. <!-- 通用连接池-->
  2. <dependency>
  3. <groupId>org.apache.commons</groupId>
  4. <artifactId>commons-pool2</artifactId>
  5. </dependency>

二、配置redis数据源

  1. # redis数据源
  2. redis:
  3. host: localhost
  4. port: 6379

三、redis序列化配置

如果不配置,就会使用默认的配置。

  1. package com.tj.demo.config;
  2. import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.data.redis.serializer.StringRedisSerializer;
  8. @Configuration
  9. public class RedisConfig {
  10. /**
  11. * 设置Redis序列化方式,默认使用的JDKSerializer的序列化方式,效率低,这里我们使用 FastJsonRedisSerializer
  12. *
  13. * @param redisConnectionFactory
  14. * @return
  15. */
  16. @Bean
  17. public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
  18. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  19. // key序列化
  20. redisTemplate.setKeySerializer(new StringRedisSerializer());
  21. // value序列化
  22. redisTemplate.setValueSerializer(new FastJsonRedisSerializer<>(Object.class));
  23. // Hash key序列化
  24. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  25. // Hash value序列化
  26. redisTemplate.setHashValueSerializer(new StringRedisSerializer());
  27. //连接工厂对象
  28. redisTemplate.setConnectionFactory(redisConnectionFactory);
  29. return redisTemplate;
  30. }
  31. }

四、业务实现

参考:RedisTemplate方法详解

1、redisTemplate

  1. redisTemplate.opsForValue();//操作String类型数据
  2. redisTemplate.opsForHash(); //操作Hash类型
  3. redisTemplate.opsForList(); //操作List类型
  4. redisTemplate.opsForSet(); //操作Set类型
  5. redisTemplate.opsForZSet(); //操作SortedSet类型

2、测试案例

  1. import org.springframework.data.redis.core.RedisTemplate;
  2. //加载redis模版
  3. @Autowired(required = false)
  4. RedisTemplate redisTemplate;
  5. @Test
  6. void testRedis() throws IOException {
  7. //设置key-value
  8. redisTemplate.opsForValue().set("tang", "ceshi123");
  9. //根据key读取value
  10. Object tang = redisTemplate.opsForValue().get("tang");
  11. log.info(tang.toString());
  12. }

五、同步代码优化

如果用redis缓存数据库数据,应该用同步代码块来优化

  1. //同步代码逻辑
  2. synchronized (this.getClass()){
  3. //这个里面是同步代码
  4. //里面要再测进行验证,缓存里是否有数据,
  5. //如果有就取缓存的,如果没有就去查数据库,并把数据库的结果再缓存到redis里面
  6. }

六、实际案例

1、JWT和redis结合的登录验证案例

参考:JWT后台实现旧Token失效的问题(添加黑名单方式)
Redis设置过期时间
TimeUnit用法
POST用户退出登录API


七、其他

1、连接redis的桌面工具

redis-desktop-manager下载连接

2、redis设置密码的方法

局域网可以不需要设置密码
在redis根目录下,打开redis.windows.conf配置文件,查找requirepass,添加密码。
image.png
注意,行前不能有空格。

  1. # requirepass foobared
  2. requirepass "abcd"

3、windows下载自动启动redis

参考:Windows设置redis开启自动启动

  1. redis-server --service-install redis.windows-service.conf --loglevel verbose
  2. //启动命令
  3. redis-server --service-start
  4. //停止服务
  5. redis-server --service-stop

image.png