diff --git a/xiaoyi-auth/pom.xml b/xiaoyi-auth/pom.xml index bfbc39d..43776a4 100644 --- a/xiaoyi-auth/pom.xml +++ b/xiaoyi-auth/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 @@ -60,10 +60,22 @@ - - cn.dev33 - sa-token-spring-boot3-starter - + + cn.dev33 + sa-token-spring-boot3-starter + + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + org.apache.commons + commons-pool2 + diff --git a/xiaoyi-auth/src/main/java/com/jy/xiaoyishu/auth/config/RedisTemplateConfig.java b/xiaoyi-auth/src/main/java/com/jy/xiaoyishu/auth/config/RedisTemplateConfig.java new file mode 100644 index 0000000..ad96868 --- /dev/null +++ b/xiaoyi-auth/src/main/java/com/jy/xiaoyishu/auth/config/RedisTemplateConfig.java @@ -0,0 +1,40 @@ +package com.jy.xiaoyishu.auth.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * + * @author ayi + * @version V1.0 + * @title RedisTemplateConfig + * @description Redis 配置类 + * @date 2026/01/12 19:13 + */ + +@Configuration +public class RedisTemplateConfig { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + // 设置 RedisTemplate 的连接工厂 + redisTemplate.setConnectionFactory(connectionFactory); + + // 使用 StringRedisSerializer 来序列化和反序列化 redis 的 key 值,确保 key 是可读的字符串 + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + + // 使用 Jackson2JsonRedisSerializer 来序列化和反序列化 redis 的 value 值, 确保存储的是 JSON 格式 + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Object.class); + redisTemplate.setValueSerializer(serializer); + redisTemplate.setHashValueSerializer(serializer); + + redisTemplate.afterPropertiesSet(); + return redisTemplate; + } +} diff --git a/xiaoyi-auth/src/main/resources/config/application-dev.yml b/xiaoyi-auth/src/main/resources/config/application-dev.yml index e0d4808..175eb32 100644 --- a/xiaoyi-auth/src/main/resources/config/application-dev.yml +++ b/xiaoyi-auth/src/main/resources/config/application-dev.yml @@ -41,6 +41,20 @@ spring: config: multi-statement-allow: true connection-properties: config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAK6C4nQHNuYSebx/5vOdvDqP/o8AH+p73s1LWCFs915RiwVHvtEd+ropmXkCO3Agc9Zuo8pyMvccIgPL9F0I8YkCAwEAAQ== + data: + redis: + database: 0 # Redis 数据库索引(默认为 0) + host: 127.0.0.1 # Redis 服务器地址 + port: 6379 # Redis 服务器连接端口 + password: 12345678 # Redis 服务器连接密码(默认为空) + timeout: 5s # 读超时时间 + connect-timeout: 5s # 链接超时时间 + lettuce: + pool: + max-active: 200 # 连接池最大连接数 + max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制) + min-idle: 0 # 连接池中的最小空闲连接 + max-idle: 10 # 连接池中的最大空闲连接 output: ansi: enabled: always diff --git a/xiaoyi-auth/src/test/java/com/jy/xiaoyishu/auth/RedisTests.java b/xiaoyi-auth/src/test/java/com/jy/xiaoyishu/auth/RedisTests.java new file mode 100644 index 0000000..0437578 --- /dev/null +++ b/xiaoyi-auth/src/test/java/com/jy/xiaoyishu/auth/RedisTests.java @@ -0,0 +1,57 @@ +package com.jy.xiaoyishu.auth; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.redis.core.RedisTemplate; + +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; + +/** + * + * @author ayi + * @version V1.0 + * @title RedisTests + * @description Redis 测试类 + * @date 2026/01/12 19:15 + */ + +@SpringBootTest +@Slf4j +public class RedisTests { + @Resource + private RedisTemplate redisTemplate; + + /** + * set key value + */ + @Test + void testSetKeyValue() { + // 添加一个 key 为 name, value 值为 刑加一 + redisTemplate.opsForValue().set("name", "刑加一"); + } + + /** + * 判断某个 key 是否存在 + */ + @Test + void testHasKey() { + log.info("key 是否存在:{}", Boolean.TRUE.equals(redisTemplate.hasKey("name"))); + } + + /** + * 获取某个 key 的 value + */ + @Test + void testGetValue() { + log.info("value 值:{}", redisTemplate.opsForValue().get("name")); + } + + /** + * 删除某个 key + */ + @Test + void testDelete() { + redisTemplate.delete("name"); + } +}