当前位置:网站首页>Use of redis in projects
Use of redis in projects
2022-06-30 12:05:00 【Not bald】
1. Introduce dependencies
org.springframework.boot
spring-boot-starter-data-redis
2. stay yml File to configure 
3.redis Related configuration classes
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 use String How to serialize
template.setKeySerializer(stringRedisSerializer);
// hash Of key Also used String How to serialize
template.setHashKeySerializer(stringRedisSerializer);
// value The serialization method adopts jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash Of value The serialization method adopts jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
4. To test
@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);
}
边栏推荐
- 1020. 飞地的数量
- led背光板的作用是什么呢?
- redis在项目中的使用
- 光谱共焦位移传感器的原理是什么?能应用那些领域?
- 麒麟软件韩乃平:数字中国建设需要自己的开源根社区
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ x_ The log10 function configures the value range of the X axis to be logarithmic coordinates
- Using cookie technology to realize historical browsing records and control the number of displays
- Goto statement jump uninitialized variable: c2362
- There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
- R language ggplot2 visualization: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
猜你喜欢
随机推荐
Object mapping - mapping Mapster
串行通信接口8250
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
Flutter 从零开始 006 单选开关和复选框
Redis - SDS simple dynamic string
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)
nvm安装node后,在使用npm指令时候显示不是内部或外部指令
构造函数、类成员、析构函数调用顺序
Typescript readonlyarray (read only array type) details
R language ggplot2 visualization: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
He was the first hero of Shanghai's two major industries, but died silently in regret
【云原生 | Kubernetes篇】深入了解Deployment(八)
In depth analysis of Apache bookkeeper series: Part 4 - back pressure
time 函数和 clock_gettime()函数的区别
Cache avalanche and cache penetration solutions
Openmldb meetup No.4 meeting minutes
Speech signal processing - Fundamentals (V): Fourier transform
麒麟软件韩乃平:数字中国建设需要自己的开源根社区
Pointdistiller: structured knowledge distillation for efficient and compact 3D detection









