推荐学习:redis视频教程
String——字符串Hash——字典List——列表Set——集合Sorted Set——有序集合redisTemplate.opsForValue();//操作字符串 redisTemplate.opsForHash();//操作hash redisTemplate.opsForList();//操作list redisTemplate.opsForSet();//操作set redisTemplate.opsForZSet();//操作有序set
RedisConfig.java
package com.wj.demo.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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
}

测试成功。

这种方式是使用list或者set这些来存储的,这样的方式其实也可以达到我们想要的效果,但是因为每次修改属性都需要三步走,性能开销非常大。1.先反序列化;2,修改;3.序列化
这种方式其实也有两种写法
写法一:

这种写法不仅能够达成目标,而且解决了资源消耗过大的问题,但是也引起了另一个问题,就是用户的id数据冗余
写法二:

通过key(用户id)+field(属性标签)可以操作对应属性数据了,既不需要重复存储数据,也不会带来序列化和并修复操控的问题
推荐学习:redis视频教程
以上就是一起聊聊Redis如何实现保存对象的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号