重生之 SpringBoot3 入门保姆级学习(20、场景整合 Docker 的 Redis 对接与基础使用)
- 6.3 整合 Redis
6.3 整合 Redis
1、创建新项目编写 application.properties 配置文件
# redis 配置
spring.data.redis.host=192.168.1.4
spring.data.redis.port=6379
- 如果不知道主机号的请执行下面代码找到 192.168 开头的局域网地址
ifconfig
2、创建 RedisTestController 测试类
package com.zhong.redis.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @ClassName : RedisTestController* @Description :* @Author : zhx* @Date: 2024-06-14 15:13*/
@RestController
public class RedisTestController {@AutowiredStringRedisTemplate redisTemplate;@GetMapping("/count")public String count() {Long count = redisTemplate.opsForValue().increment("count");// TODO 常见数据类型 k:v value可以有很多类型//// string: 普通字符串: redisTemplate.opsForValue()// list 列表: redisTemplate.opsForList()// set: 集合: redisTemplate.opsForset()// zset: 有序集合: redisTemplate.opsForzset()// hash: map结构: redisTemplate.opsForHash()return "访问了[ " + count + " ]次";}
}
3、运行后浏览器访问 地址 刷新测试
http://localhost:8080/count
4、查看虚拟机的 Docker 中的 Redis 是否响应
- Redis 的常用操作演示
package com.zhong.redis;import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;@SpringBootTest
class Boot307RedisApplicationTests {// TODO 常见数据类型 k:v value可以有很多类型//// string: 普通字符串: redisTemplate.opsForValue()// list 列表: redisTemplate.opsForList()// set: 集合: redisTemplate.opsForset()// zset: 有序集合: redisTemplate.opsForzset()// hash: map结构: redisTemplate.opsForHash()@AutowiredStringRedisTemplate redisTemplate;@Testvoid testSting() {redisTemplate.opsForValue().set("name", "张三");String name = redisTemplate.opsForValue().get("name");System.out.println(name);}@Testvoid testList() {String listName = "listtest";redisTemplate.opsForList().leftPush(listName, "1");redisTemplate.opsForList().leftPush(listName, "2");redisTemplate.opsForList().leftPush(listName, "3");String leftPop = redisTemplate.opsForList().leftPop(listName);Assertions.assertEquals("3", leftPop);}@Testvoid testSet() {String setName = "settest";redisTemplate.opsForSet().add(setName, "1");redisTemplate.opsForSet().add(setName, "2");redisTemplate.opsForSet().add(setName, "2");redisTemplate.opsForSet().add(setName, "2");redisTemplate.opsForSet().add(setName, "3");Boolean aBoolean = redisTemplate.opsForSet().isMember(setName, "2");Boolean bBoolean = redisTemplate.opsForSet().isMember(setName, "5");Assertions.assertTrue(aBoolean);Assertions.assertFalse(bBoolean);}@Testvoid testZset() {String setName = "zsettest";redisTemplate.opsForZSet().add(setName, "小钟", 100);redisTemplate.opsForZSet().add(setName, "小王", 99);redisTemplate.opsForZSet().add(setName, "小小", 1);redisTemplate.opsForZSet().add(setName, "小大", 2);ZSetOperations.TypedTuple<String> popMax = redisTemplate.opsForZSet().popMax(setName);String value = popMax.getValue();Double score = popMax.getScore();System.out.println(value + "====>" + score);}@Testvoid testHash() {String setName = "amap";redisTemplate.opsForHash().put(setName,"name","张三");redisTemplate.opsForHash().put(setName,"age","18");Object name = redisTemplate.opsForHash().get(setName, "name");System.out.println(name);}}
- 运行结果
https://blog.csdn.net/weixin_56050344/article/details/139242706?spm=1001.2014.3001.5502