1. 问题所示
某些信息存储在缓存中,对此修改缓存,想通过可视化的redis desktop来操作,但是发现都是二进制的数据,不好操作:

于是想着以命令行的方式修改,但又怕改错,毕竟都是难以读懂的二进制数据,如图所示:(由于信息比较隐私,此处打码了)

2. 原理分析
redistemplate 存储的数据通常是二进制的。redis 是一个键值存储系统,它存储的值可以是字符串、二进制数据、甚至是序列化的对象。
redistemplate 是 spring data redis 提供的一个类,用于在 spring 应用中与 redis 进行交互。
它提供了一种在 java 对象和 redis 数据之间进行序列化和反序列化的机制。
默认情况下,redistemplate 使用 jdk 的序列化机制将 java 对象转换为二进制数据,并存储在 redis 中。
3. 拓展
在使用redis作为数据存储时,合理的键值序列化配置和redistemplate的使用方式对于应用性能和数据可维护性至关重要。
默认情况下,spring data redis使用jdkserializationredisserializer作为redistemplate的默认序列化器
这意味着键和值都将以java对象的二进制形式进行序列化,并以字节流的方式存储在redis中。
不对值进行额外序列化的理由:虽然值以二进制形式存储,但在java代码中获取值时会进行反序列化,因此这不会影响业务逻辑和数据的正确性。
同时,对值进行额外的序列化可能会限制存储的数据类型,例如将值序列化为字符串将仅能存储字符串类型的值。额外序列化的场景
如果需要存储复杂的数据结构或非字符串类型的值,可以选择配置redistemplate使用其他序列化器,例如json序列化器或自定义序列化器。- 这样可以更灵活地存储各种类型的数据,但需要注意在读取时正确地反序列化。
特殊值的处理
对于包含特殊类型(如localdatetime、localdate、localtime等)的值,可能需要注意转换为字符串或选择合适的序列化方式。
通过理解redis默认行为和灵活运用redistemplate,可以更好地应对不同类型数据的存储需求,从而优化系统性能并提升开发效率
给出一个demo代码更加容易懂:
先引入这两个依赖包:
<dependency>
<groupid>org.springframework.data</groupid>
<artifactid>spring-data-redis</artifactid>
<version>2.6.5.release</version> <!-- 使用你项目中的实际版本 -->
</dependency>
<dependency>
<groupid>redis.clients</groupid>
<artifactid>jedis</artifactid>
<version>3.7.0</version> <!-- 使用你项目中的实际版本 -->
</dependency>
对key进行可视化:
import org.springframework.data.redis.connection.jedis.jedisconnectionfactory;
import org.springframework.data.redis.core.hashoperations;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.data.redis.serializer.stringredisserializer;
public class demo {
public static void main(string[] args) {
// 创建 redis 连接配置
jedisconnectionfactory jedisconnectionfactory = new jedisconnectionfactory();
jedisconnectionfactory.sethostname("localhost");
jedisconnectionfactory.setport(6379);
// jedisconnectionfactory.setpassword(redispassword.of("your-redis-password"));
// 手动调用 afterpropertiesset()
jedisconnectionfactory.afterpropertiesset();
// 创建 redistemplate 实例
redistemplate<string, object> redistemplate = new redistemplate<>();
//设置string类型的key设置序列化器
redistemplate.setkeyserializer(new stringredisserializer());
//设置hash类型的key设置序列化器
redistemplate.sethashkeyserializer(new stringredisserializer());
redistemplate.setconnectionfactory(jedisconnectionfactory);
redistemplate.afterpropertiesset();
// 存储字符串
redistemplate.opsforvalue().set("message", "hello, redis!");
// 获取字符串
string message = (string) redistemplate.opsforvalue().get("message");
system.out.println("message: " + message);
// 存储哈希
hashoperations<string, string, string> hashoperations = redistemplate.opsforhash();
hashoperations.put("user", "id", "1");
hashoperations.put("user", "name", "码农研究僧");
// 获取哈希
string userid = hashoperations.get("user", "id");
string username = hashoperations.get("user", "name");
system.out.println("user id: " + userid);
system.out.println("user name: " + username);
}
}
如果不对key可视化,只需要删除这两行代码即可:
//设置string类型的key设置序列化器 redistemplate.setkeyserializer(new stringredisserializer()); //设置hash类型的key设置序列化器 redistemplate.sethashkeyserializer(new stringredisserializer());
截图如下:

以上就是详解redis中数值乱码的根本原因以及解决方式的详细内容,更多关于redis数值乱码的资料请关注代码网其它相关文章!
发表评论