当前位置:网站首页>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);
}
边栏推荐
- wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
- Flutter 从零开始 007 输入框
- 治数如治水,数据治理和数据创新难在哪?
- Embedded SIG | 多 OS 混合部署框架
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_x_log10函数配置X轴的数值范围为对数坐标
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_color_viridis_d函数指定数据点的配色方案
- HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
- Customize an annotation to get a link to the database
- MySQL 内置函数
- Summer vacation study record
猜你喜欢
随机推荐
wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
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
智慧法院新征程,无纸化办公,护航智慧法院绿色庭审
Boost study: boost log
他是上海两大产业的第一功臣,却在遗憾中默默离世
网络营销之四大误解
R language ggplot2 visual Facet: gganimate package is based on Transition_ The time function creates a dynamic scatter graph animation (GIF) and uses the labs function to add a dynamic time title to t
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)
R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置alpha参数指定数据点的透明度级别(points transparent、从0到1)
time 函数和 clock_gettime()函数的区别
Automatic database growth
led背光板的作用是什麼呢?
Openmldb meetup No.4 meeting minutes
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set the alpha parameter in the point parameter to specify the transparency level of data points (points transparent
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
串行通信接口8250
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题
Multiparty cardinality testing for threshold private set-2021: Interpretation
wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
爱可可AI前沿推介(6.30)








