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

Case Analysis of Springboot Integrated Redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this "Springboot integration Redis case analysis" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Springboot integration Redis case analysis" article.

Dependency package

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

Configuration file (application.properties)

# Redis database index (default is 0) spring.redis.database=0# Redis server address spring.redis.host=x.x.x.x# Redis server connection port spring.redis.port=6738# Redis server connection password (default is empty) spring.redis.password=# connection timeout (milliseconds) spring.redis.timeout=10000# connection pool maximum number of connections (use negative values for unlimited) spring.redis.jedis.pool.max-active=8 # connection pool maximum blocking wait time (using negative values for unlimited) maximum idle connections in spring.redis.jedis.pool.max-wait=-1ms# connection pool minimum idle connection spring.redis.jedis.pool.min-idle=0 in spring.redis.jedis.pool.max-idle=8# connection pool

Configuration file (RedisConfig.java)

Package com.gxr.dmsData.config;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.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer Import java.text.SimpleDateFormat;/** * @ author: gongxr * @ description: custom RedisTemplate * @ date: Created in 2021-6-30 * / @ Configurationpublic class RedisConfig {@ Bean public RedisTemplate redisTemplate (RedisConnectionFactory redisConnectionFactory) {RedisTemplate redisTemplate = new RedisTemplate (); redisTemplate.setConnectionFactory (redisConnectionFactory); / / modify key's default serializer to stringRedisSerializer stringRedisSerializer = new StringRedisSerializer (); redisTemplate.setDefaultSerializer (stringRedisSerializer) / / Custom object conversion ObjectMapper objectMapper = new ObjectMapper (); objectMapper.setDateFormat (new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss")); objectMapper.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); Jackson2JsonRedisSerializer valueSerializer = new Jackson2JsonRedisSerializer (Object.class); valueSerializer.setObjectMapper (objectMapper); / / redisTemplate.setValueSerializer (valueSerializer) / / redisTemplate.setHashValueSerializer (valueSerializer); redisTemplate.afterPropertiesSet (); return redisTemplate;}}

Test code

Import com.gxr.dmsData.common.BaseTest;import lombok.extern.slf4j.Slf4j;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import java.util.Set;/** * @ author: gongxr * @ description: * @ date: Created in 2021-6-30 * / @ Slf4jpublic class TestRedis extends BaseTest {@ Autowired private RedisTemplate redisTemplate / * RedisTemplate defines operations on five data structures * redisTemplate.opsForValue (); / / operation string * redisTemplate.opsForHash (); / operation hash * redisTemplate.opsForList (); / operation list * redisTemplate.opsForSet (); / / operation set * redisTemplate.opsForZSet (); / / orderly operation set * / @ Test public void testRedisGet () {String key = "adviceCalculateTime" Boolean b = redisTemplate.hasKey (key); log.info ("whether key exists: {}", b); Object o = redisTemplate.boundValueOps (key). Get (); log.info (redisTemplate.toString ()); log.info ("query results: {}", o) } / * * map type * / @ Test public void testRedisHash () {String key = "RRS_CURRENCY_CACHE"; Object o = redisTemplate.boundHashOps (key) .get ("590"); log.info ("query result: {}", o.toString ()) } / * set type * / @ Test public void testRedisSet () {String key = "goodsDataSyncSkc:set"; Set set = redisTemplate.boundSetOps (key). Members (); log.info ("query result: {}", set.size ()); String s = (String) redisTemplate.boundSetOps (key). RandomMember (); log.info ("query result: {}", s) }} the above is about the content of the article "Springboot Integration Redis instance Analysis". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report