当前位置:网站首页>redis在项目中的使用
redis在项目中的使用
2022-06-30 11:41:00 【qingqing不秃头】
1.引入依赖
org.springframework.boot
spring-boot-starter-data-redis
2.在yml文件中进行配置
3.redis相关配置类
package com.redis.confic;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
4.进行测试
@Test
public void demo2() {
User user = new User("zrq",18);
Set<String> keys = redisTemplate.keys("*");
redisTemplate.delete(keys);
redisTemplate.opsForValue().set("user", JSON.toJSONString(user));
String user1 = redisTemplate.opsForValue().get("user");
System.out.println(user1);
User user2 = JSON.parseObject(String.valueOf(user1), User.class);
System.out.println(user2);
}
边栏推荐
- R language de duplication operation unique duplicate filter
- Uncover the whole link communication process of customer service im
- 对象映射 - Mapping.Mapster
- Yolov5 export the pit encountered by onnx
- Typescript readonlyarray (read only array type) details
- Re understand oauth2.0 protocol for joint login
- 1175. prime permutation
- AUTOCAD——LEN命令
- Database cascading operation
- 线下门店为什么要做新零售?
猜你喜欢
1175. prime permutation
A theoretical defect of relative position coding transformer and Its Countermeasures
R language view version R package view version
"War" caused by a bottle of water
The operation and maintenance security gateway (Fortress machine) of Qiming star group once again won the first place!
Redis - ziplist compressed list
服务器常用的一些硬件信息(不断更新)
led背光板的作用是什麼呢?
There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
随机推荐
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题(抽取frame_time信息)
[revisiting the classic C language] ~x,%c,%d,%x, etc. in C language, the role of the address character in C language, and the consortium in C language
TypeScript ReadonlyArray(只读数组类型) 详细介绍
Let's talk about how to do hardware compatibility testing and quickly migrate to openeuler?
shell第一个命令结果传入第二个命令删除
"New digital technology" completed tens of millions of yuan of a + round financing and built an integrated intelligent database cloud management platform
使用深度学习进行生物网络分析
Yolov5 export the pit encountered by onnx
线下门店为什么要做新零售?
YOLOv5导出onnx遇到的坑
又被 Kotlin 语法糖坑惨的一天
数据库 事务
It is said that with this, the boss opened the test overnight
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的size参数指定数据点的大小(point size)
重新理解oauth2.0协议进行联合登录
R language view version R package view version
治数如治水,数据治理和数据创新难在哪?
Set up your own website (13)
Oracle netsuite helps TCM bio understand data changes and make business development more flexible
Goto statement jump uninitialized variable: c2362