当前位置:网站首页>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);
}
边栏推荐
- R language ggplot2 visualization: use ggplot2 visualization scatter diagram and the color parameter in AES function to specify that data points in different groups are displayed in different colors
- 【重温经典C语言】~c语言中%x、%c、%d、%x等等等、c语言取地址符&的作用、C语言中的 联合体
- ModelAtrs声音检测,基于声学特征的异响检测
- zabbix监控TCP连接个数
- 麒麟软件韩乃平:数字中国建设需要自己的开源根社区
- 使用深度学习进行生物网络分析
- 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
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set show in the point parameter_ The legend parameter is false, and the legend information is not displayed
- Shell first command result is transferred to the second command delete
- wallys/3 × 3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
猜你喜欢
随机推荐
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
Using cookie technology to realize historical browsing records and control the number of displays
R language ggplot2 visualization: use ggplot2 visualization scatter diagram and the size parameter in AES function to specify the size of data points (point size)
A Generic Deep-Learning-Based Approach for Automated Surface Inspection-論文閱讀筆記
zabbix监控TCP连接个数
Shutter 007 input field from zero
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
Go 语言入门很简单:Go 处理 XML 文件
Serial communication interface 8250
HMS core audio editing service 3D audio technology helps create an immersive auditory feast
Beego development blog system learning (II)
Let's talk about how to do hardware compatibility testing and quickly migrate to openeuler?
8253 counter introduction
会议预告 | 华为 2012 实验室全球软件技术峰会-欧洲分会场
How can c write an SQL parser
他是上海两大产业的第一功臣,却在遗憾中默默离世
led背光板的作用是什麼呢?
VScode选中多个单词
Multiparty Cardinality Testing for Threshold Private Set-2021:解读
Goto statement jump uninitialized variable: c2362