這篇“Springboot集成Redis實例分析”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Springboot集成Redis實例分析”文章吧。
依賴包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
配置文件(application.properties)
# Redis數據庫索引(默認為0) spring.redis.database=0 # Redis服務器地址 spring.redis.host=x.x.x.x # Redis服務器連接端口 spring.redis.port=6738 # Redis服務器連接密碼(默認為空) spring.redis.password= # 連接超時時間(毫秒) spring.redis.timeout=10000 # 連接池最大連接數(使用負值表示沒有限制) spring.redis.jedis.pool.max-active=8 # 連接池最大阻塞等待時間(使用負值表示沒有限制) spring.redis.jedis.pool.max-wait=-1ms # 連接池中的最大空閑連接 spring.redis.jedis.pool.max-idle=8 # 連接池中的最小空閑連接 spring.redis.jedis.pool.min-idle=0
配置文件(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: 自定義RedisTemplate * @date :Created in 2021/6/30 */ @Configuration public class RedisConfig { @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); // 修改key的默認序列化器為 string RedisSerializer<String> stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setDefaultSerializer(stringRedisSerializer); // 自定義 對象轉換 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<Object> valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class); valueSerializer.setObjectMapper(objectMapper); // redisTemplate.setValueSerializer(valueSerializer); // redisTemplate.setHashValueSerializer(valueSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } }
測試代碼
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 */ @Slf4j public class TestRedis extends BaseTest { @Autowired private RedisTemplate redisTemplate; /** * RedisTemplate中定義了對5種數據結構操作 * redisTemplate.opsForValue();//操作字符串 * redisTemplate.opsForHash();//操作hash * redisTemplate.opsForList();//操作list * redisTemplate.opsForSet();//操作set * redisTemplate.opsForZSet();//操作有序set */ @Test public void testRedisGet() { String key = "adviceCalculateTime"; Boolean b = redisTemplate.hasKey(key); log.info("key是否存在:{}", b); Object o = redisTemplate.boundValueOps(key).get(); log.info(redisTemplate.toString()); log.info("查詢結果:{}", o); } /** * map類型 */ @Test public void testRedisHash() { String key = "RRS_CURRENCY_CACHE"; Object o = redisTemplate.boundHashOps(key).get("590"); log.info("查詢結果:{}", o.toString()); } /** * set類型 */ @Test public void testRedisSet() { String key = "goodsDataSyncSkc:set"; Set set = redisTemplate.boundSetOps(key).members(); log.info("查詢結果:{}", set.size()); String s = (String) redisTemplate.boundSetOps(key).randomMember(); log.info("查詢結果:{}", s); } }
以上就是關于“Springboot集成Redis實例分析”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。