日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot2.5.0 整合 redis 配置详解

發布時間:2024/8/26 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot2.5.0 整合 redis 配置详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. pom添加依賴

?<!--redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>

2.?application.properties 配置文件

#===========Redis配置=========== #?Redis數據庫索引(默認為0)?? spring.redis.database=0 #?Redis服務器地址?? spring.redis.host=127.0.0.1 #?Redis服務器連接端口?? spring.redis.port=6379 #?Redis服務器連接密碼 spring.redis.password=root #?連接池最大連接數(使用負值表示沒有限制)?? spring.redis.pool.max-active=200 #?連接池最大阻塞等待時間(使用負值表示沒有限制)?? spring.redis.pool.max-wait=-1 #?連接池中的最大空閑連接? spring.redis.pool.max-idle=10 #?連接池中的最小空閑連接?? spring.redis.pool.min-idle=0 #?連接超時時間(毫秒) spring.redis.timeout=2000ms spring.redis.jedis.pool.max-wait=-1ms #===========Redis配置===========

3.?RedisConfig.java 配置類

package?org.fh.config; 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; import?com.fasterxml.jackson.databind.ObjectMapper; import?com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import?com.fasterxml.jackson.annotation.PropertyAccessor; import?com.fasterxml.jackson.annotation.JsonAutoDetect; /***??說明:Redis*?from:www?fhadmin?org*/ @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.activateDefaultTyping(LaissezFaireSubTypeValidator.instance?,?ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);StringRedisSerializer?stringRedisSerializer?=?new?StringRedisSerializer();//?key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);//?hash的key也采用String的序列化方式template.setHashKeySerializer(stringRedisSerializer);//?value序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);//?hash的value序列化方式采用jacksontemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return?template;} }

4. 調用redis

???@Autowiredprivate?RedisTemplate<String,?Object>?redisTemplate;/***?普通緩存獲取*?@param?key?鍵*?@return?值*/public?Object?get(String?key)?{return?key?==?null???null?:?redisTemplate.opsForValue().get(key);}/***?普通緩存放入*?@param?key?鍵*?@param?value?值*?@return?true成功?false失敗*/public?boolean?set(String?key,?Object?value)?{try?{redisTemplate.opsForValue().set(key,?value);return?true;}?catch?(Exception?e)?{//e.printStackTrace();return?false;}}

?

總結

以上是生活随笔為你收集整理的springboot2.5.0 整合 redis 配置详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。