redis服务压缩包(windows版):Redis-x64-3.2.100.zip
链接:https://pan.baidu.com/s/1fGdrmCjnj3bXKnA7GanoGw
提取码:1111
亲情提示:可将server发送快捷方式到桌面,方便调用
redis可视化软件:redis-desktop-manager-0.8.8.384
链接:https://pan.baidu.com/s/1b_9OI_6Oo1WeRTgipyMlAw
提取码:1111
无脑下一步(选下安装路径,省的c盘爆满)
代码部分 第一步:引依赖第二步:配配置org.springframework.boot spring-boot-starter-redis1.4.7.RELEASE
spring:
redis:
host: 127.0.0.1
# port: 6379
# password:
# database: 0
第三步:test测试
1.引依赖
2.写test类org.springframework.boot spring-boot-starter-testjunit junit4.13.2 test
package com.my;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApp.class)
public class Test {
@Autowired
private RedisTemplate redisTemplate;
@org.junit.Test
public void test() {
// 测试value
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("name",123);
Object name = valueOperations.get("name");
System.out.println(name);
// 测试hash
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("dictionary","querySwitch",true);
Object querySwitch = hashOperations.get("dictionary", "querySwitch");
System.out.println(querySwitch);
}
}
3.运行结果
没有毛病
但是当我想打开可视化软件看下存储的结果和结构的时候
这咋都是乱码?
只能借助度娘了
第四步:Redis配置使用RedisTemplate存储至缓存数据乱码解决_呢喃北上的博客-CSDN博客
网上有很多类似的博客,贴上其中一个大佬的博客↑↑↑
增加一个配置类 RedisConfig
package com.my.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Autowired
private RedisTemplate redisTemplate;
@Bean
public RedisTemplate redisTemplateInit() {
// utf-8
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// jackson2
GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
// 设置序列化value
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// 设置序列化hash
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
}
一开始我也是都用的StringRedisSerializer的序列化方式,但是它会将value都转成string,当你存string以外类型时,会出现转换问题
所以只有key用的是StringRedisSerializer,value用的都是GenericJackson2JsonRedisSerializer
解决乱码问题
后来了解到,其实不是乱码,而是RedisTemplate默认的jdk序列化策略,虽然暂时不影响存取值,但是不利于排查,感觉还是改成后面这样的比较好。
感谢大家的支持和指教,希望可以一起学习进步♠♠♠



