Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Redis in SpringBoot Integration

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use Redis in SpringBoot integration. I hope you will get something after reading this article. Let's discuss it together.

SpringBoot integration using redis

Jedis is a Java-oriented client officially launched by Redis, which provides many interfaces for Java language to call. Can be downloaded from the official website of Redis. Spring-data-redis is part of the spring family, which provides access to redis services through simple configuration in srping applications, highly encapsulated reids underlying development packages (Jedis, JRedis, and RJC), and RedisTemplate provides various redis operations

Spring-data-redis provides the following features for jedis:

Connection pooling is automatically managed, providing a highly encapsulated "RedisTemplate" class.

A large number of api in jedis client are classified and encapsulated, and the same type of operation is encapsulated as operation interface.

ValueOperations: simple Kmurv operation

SetOperations:set type data operation

ZSetOperations:zset type data operation

HashOperations: data manipulation for map types

ListOperations: data manipulation for list types

3. The transaction operation is encapsulated and controlled by the container.

4. A variety of alternative strategies (RedisSerializer) are provided for serialization / deserialization of data

JdkSerializationRedisSerializer:POJO object access scenario, using JDK's own serialization mechanism.

When StringRedisSerializer:Key or value is a string, the byte sequence of the data is encoded into string according to the specified charset, which is the direct encapsulation of "new String (bytes, charset)" and "string.getBytes (charset)". Is the most lightweight and efficient strategy.

The JacksonJsonRedisSerializer:jackson-json tool provides the conversion ability between javabean and json, which can serialize pojo instances into json format and store them in redis, or convert json format data into pojo instances.

Build 1. Import jar package

Org.springframework.boot spring-boot-starter-data-redis

two。 Configure connection redis

Spring: redis: host: 192.168.31.100 port: 6379 password: 111database: 0 pool: max-active: 8 # maximum number of connections in the connection pool (using negative values for no limit) max-wait:-1ms # maximum blocking wait time in the connection pool (using negative values for no limit) max-idle: maximum in the connection pool Idle connection min-idle: 0 # minimum idle connection in the connection pool timeout: 5000ms # connection timeout (milliseconds)

Add the above configuration under spring in the application.yml file

3. Add configuration class RedisConfig

Package com.ffyc.back.demo.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.JsonTypeInfo;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.DeserializationFeature;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;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;@Configurationpublic class RedisConfig {/ * serialized key, value * @ param connectionFactory * @ return * / @ Bean public RedisTemplate redisTemplate (RedisConnectionFactory connectionFactory) {RedisTemplate redisTemplate = new RedisTemplate (); redisTemplate.setConnectionFactory (connectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class); StringRedisSerializer redisSerializer = new StringRedisSerializer () RedisTemplate.setKeySerializer (redisSerializer); redisTemplate.setHashKeySerializer (redisSerializer); redisTemplate.setValueSerializer (jackson2JsonRedisSerializer); redisTemplate.setHashValueSerializer (jackson2JsonRedisSerializer); return redisTemplate;}}

Add this configuration to the configuration package

The function of this configuration class is to json the data to be passed by the backend. If there is no such configuration, the format passed by the backend and the discrepancy between the redised end and the backend will result in garbled code.

4. Inject RedisTemplate

It can be used after injection where it is needed.

5. Testing and use

Examples of use:

(1)

(2)

After reading this article, I believe you have a certain understanding of "SpringBoot Integration how to use Redis". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report