Spring-Data-Redis
操作 Redis 的 api
Springboot
下使用 Spring-Data-Redis
插入数据 key 乱码
解决办法:
查看 自动配置类ctrl + f
//自定义序列化方式
RedisSerializer<String> stringSerializer = new StringRedisSerializer(Charset.forName("GBK"));
template.setKeySerializer(stringSerializer);
template.setValueSerializer(stringSerializer);
template.setHashKeySerializer(stringSerializer);
template.setHashValueSerializer(stringSerializer);
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* @author CodeHaywire
*/
@SpringBootApplication
@ComponentScan({"com.example.config"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.nio.charset.Charset;
/**
* @ClassName : RedisSerializerConfig
* @Description : 自定义序列化
* @Author : CodeHaywire
* @Created at 2022/4/24 17:14
*/
@Configuration
public class RedisSerializerConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//自定义序列化方式
RedisSerializer<String> stringSerializer = new StringRedisSerializer(Charset.forName("GBK"));
template.setKeySerializer(stringSerializer);
template.setValueSerializer(stringSerializer);
template.setHashKeySerializer(stringSerializer);
template.setHashValueSerializer(stringSerializer);
return template;
}
}
package com.example.demo;
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.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootTest
class DemoApplicationTests {
//注入 RedisTemplate 线程是安全的
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("你好", "你好");
System.out.println(valueOperations.get("你好"));
}
}
cmd 查看 key 中文乱码:不使用 redis-cli
换成 redis-cli --raw
E:\idea-workspace\learn-spring-data-redis>redis-cli
127.0.0.1:6379> keys *
1) "\xc4\xe3\xba\xc3"
E:\idea-workspace\learn-spring-data-redis>redis-cli --raw
127.0.0.1:6379> keys *
你好
127.0.0.1:6379> get 你好
你好
tip:正常情况下也不会
key
使用中文
乱码影响查看,但是不影响编码
RedisTemplate的使用方法
important
String类型的常用方法
//键值同时操作redis 很像cmd操作redis
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("name", "ZanSan");
//先绑定键再操作redis的值 假如user需要多次操作,key可以不重复写 set("name", "ZanSan");
BoundValueOperations user = redisTemplate.boundValueOps("name");
user.set("ZanSan");
以上两种都可以插入数据,哪个方便哪个来
package com.example.demo;
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.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class DemoApplicationTests {
//注入 线程是安全的
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
BoundValueOperations user = redisTemplate.boundValueOps("name");
//设置过期时间 10s过期
user.set("CodeHaywire",10, TimeUnit.SECONDS);
}
}
C:\Users\CodeHaywire>redis-cli --raw
127.0.0.1:6379> keys *
name
127.0.0.1:6379> get name
CodeHaywire
127.0.0.1:6379> keys * #这是10s后的操作
127.0.0.1:6379>