//首先注入redisTemplate
@Autowired
private RedisTemplate<String, Object> redisTemplate;
//获取hash类型的ops
HashOperations<String, String, Object> stringObjectObjectHashOperations = redisTemplate.opsForHash();
//创建一个一个map并将一个对象的属性拆解进去
HashMap<String, Object> map = new HashMap<>();
map.put("username", user.getUsername());
map.put("password", user.getPassword());
map.put("tel", user.getTel());
map.put("address", user.getAddress());
map.put("imageFilename", user.getImageFilename());
map.put("email", user.getEmail());
map.put("coins", user.getCoins());
stringObjectObjectHashOperations.putAll(user.getId().toString(), map);
//为刚刚设置的key设置过期时间为6小时
redisTemplate.expire(user.getId().toString(), 6, TimeUnit.HOURS);
在项目中使用redisTemplate向redis添加hash类型数据
才艺のblog2024-05-08 23:37
相关推荐
用户83071968408215 小时前
Spring Boot 集成 RabbitMQ :8 个最佳实践,杜绝消息丢失与队列阻塞Java水解15 小时前
Spring Boot 视图层与模板引擎Java水解16 小时前
一文搞懂 Spring Boot 默认数据库连接池 HikariCP洋洋技术笔记19 小时前
Spring Boot Web MVC配置详解雨中飘荡的记忆1 天前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案初次攀爬者2 天前
Kafka 基础介绍用户8307196840822 天前
spring ai alibaba + nacos +mcp 实现mcp服务负载均衡调用实战Java水解2 天前
SpringBoot3全栈开发实战:从入门到精通的完整指南曲幽2 天前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案初次攀爬者2 天前
RocketMQ在Spring Boot上的基础使用