SpringBoot整合redis

      網(wǎng)友投稿 1007 2025-04-02

      Spring Data Redis介紹

      Spring Data Redis 是更大的 Spring Data 系列的一部分,它提供了從 Spring 應(yīng)用程序?qū)?Redis 的輕松配置和訪問。它提供了與商店交互的低級(jí)和高級(jí)抽象,使用戶擺脫了對(duì)基礎(chǔ)設(shè)施的擔(dān)憂。

      SpringBoot2.x版本,其內(nèi)置的Redis中間件再也不是Jedis了,而是換成了lettuce。

      lettuce: Lettuce 是 一種可伸縮,線程安全,完全非阻塞的Redis客戶端,多個(gè)線程可以共享一個(gè)RedisConnection,它利用Netty NIO 框架來(lái)高效地管理多個(gè)連接,從而提供了異步和同步數(shù)據(jù)訪問方式,用于構(gòu)建非阻塞的反應(yīng)性應(yīng)用程序。

      Jedis: Jedis 在實(shí)現(xiàn)上是直連 redis server,多線程環(huán)境下非線程安全,除非使用連接池,為每個(gè) redis實(shí)例增加 物理連接。 這種方式更加類似于我們 BIO 一條線程連一個(gè)客戶端,并且是阻塞式的,會(huì)一直連接著客戶端等待客戶端的命令

      官方文檔:https://docs.spring.io/spring-data/redis/docs/current/reference/html/#reference

      SpringBoot整合redis

      引依賴

      org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-redis org.apache.commons commons-pool2 com.fasterxml.jackson.core jackson-core

      寫配置-對(duì)于連接的管理

      spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.database=0 #超時(shí)時(shí)間 spring.redis.timeout= 1800000 # 連接池最大連接數(shù) spring.redis.lettuce.pool.max-active= 20 # 最大阻塞等待時(shí)間 spring.redis.lettuce.pool.max-wait=-1 # 連接池的最大空閑連接 spring.redis.lettuce.pool.max-idle=5 # 連接池的最小空閑連接 spring.redis.lettuce.pool.min-idle=0

      配置類-做序列化的設(shè)置

      import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; 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.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; /** * redis配置類,對(duì)key,value進(jìn)行序列化 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/5/6 - 16:49 */ @EnableCaching @Configuration public class RedisConfig { @Bean public RedisTemplate redisTemplate(RedisConnectionFactory factory) { RedisTemplate template = new RedisTemplate<>(); RedisSerializer redisSerializer = new StringRedisSerializer(); 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); template.setConnectionFactory(factory); //key序列化方式 template.setKeySerializer(redisSerializer); //value序列化 template.setValueSerializer(jackson2JsonRedisSerializer); //value hashmap序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); //解決查詢緩存轉(zhuǎn)換異常的問題 ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置序列化(解決亂碼的問題),過期時(shí)間600秒 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(600)) //設(shè)置數(shù)據(jù)過期時(shí)間600秒 .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; } }

      簡(jiǎn)單使用redis

      package com.promsing.redis.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 測(cè)試redis的操作 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/5/6 - 16:56 */ @RestController @RequestMapping("redis") public class RedisController { @Autowired private RedisTemplate redisTemplate; @GetMapping public String saveRedis(){ redisTemplate.opsForValue().set("springboot:redis:name","promsing"); Object o = redisTemplate.opsForValue().get("springboot:redis:name"); System.out.println("redis中存儲(chǔ)的內(nèi)容"+o); return o.toString(); } }

      其他測(cè)試方法

      @RunWith(SpringRunner.class) @SpringBootTest(classes = UserApplication.class) public class RedisTest { @Autowired private StringRedisTemplate redisTemplate;//存儲(chǔ)的數(shù)據(jù)進(jìn)行了序列化,建議使用它 private RedisTemplate redisTemplate2;//存儲(chǔ)的數(shù)據(jù)沒有給序列化 @Test public void testRedis() { // 存儲(chǔ)數(shù)據(jù) this.redisTemplate.opsForValue().set("key2", "value2"); // 獲取數(shù)據(jù) String val = this.redisTemplate.opsForValue().get("key2"); System.out.println("val = " + val); } @Test public void testRedis2() { // 存儲(chǔ)數(shù)據(jù),并指定剩余生命時(shí)間,5小時(shí) this.redisTemplate.opsForValue().set("key2", "value2", 5, TimeUnit.HOURS); } @Test public void testHash() { BoundHashOperations hashOps = this.redisTemplate.boundHashOps("user"); // 操作hash數(shù)據(jù) hashOps.put("name", "jack"); hashOps.put("age", "21"); // 獲取單個(gè)數(shù)據(jù) Object name = hashOps.get("name"); System.out.println("name = " + name); // 獲取所有數(shù)據(jù) Map map = hashOps.entries(); for (Map.Entry me : map.entrySet()) { System.out.println(me.getKey() + " : " + me.getValue()); } } }

      Redis Spring Boot

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:Word文檔中的某些頁(yè)面而不是所有頁(yè)面上插入水印
      下一篇:手動(dòng)目錄比較很費(fèi)力 如何在WPS文本中實(shí)現(xiàn)自動(dòng)目錄生成
      相關(guān)文章
      亚洲人成网亚洲欧洲无码久久 | 亚洲精品无码久久久久秋霞 | 亚洲国产精品一区二区三区在线观看| 亚洲国产精品一区二区九九 | 亚洲国产精品嫩草影院| 亚洲制服丝袜中文字幕| 亚洲一区在线免费观看| 亚洲妓女综合网99| 911精品国产亚洲日本美国韩国| 亚洲自偷自偷图片| 亚洲人成网77777色在线播放 | 亚洲欧美精品午睡沙发| 亚洲日本天堂在线| 亚洲成a人片在线不卡一二三区| 亚洲色成人网站WWW永久四虎| 亚洲精品人成网线在线播放va| 亚洲精品久久无码av片俺去也| 久久亚洲AV成人无码国产电影| 偷自拍亚洲视频在线观看| 亚洲av午夜成人片精品电影| 亚洲女同成人AⅤ人片在线观看| 亚洲欧洲精品成人久久曰影片| 亚洲综合色视频在线观看| 77777亚洲午夜久久多人| 亚洲国产精品无码久久SM| 亚洲免费精彩视频在线观看| 久久久亚洲AV波多野结衣| 亚洲依依成人精品| 亚洲日韩国产一区二区三区在线| 亚洲AV成人精品一区二区三区| 亚洲国产精品尤物yw在线| 亚洲综合精品香蕉久久网| 亚洲AV中文无码乱人伦下载| 亚洲精品综合久久中文字幕| 亚洲专区一路线二| 亚洲欧美成人综合久久久| 一区国严二区亚洲三区| 中文亚洲AV片在线观看不卡| 亚洲福利视频导航| 亚洲中文无码av永久| 亚洲s码欧洲m码吹潮|