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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Redis - Spring Data Redis 操作 Jedis 、Lettuce 、 Redisson

發布時間:2025/3/21 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Redis - Spring Data Redis 操作 Jedis 、Lettuce 、 Redisson 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 官網
  • Jedis VS Lettuce
  • Jedis Code
    • POM依賴
    • 配置文件
    • 配置類
    • 單元測試
  • Lettuce Code
  • Redisson Code
    • POM依賴
    • 配置文件
    • 配置類
    • 單元測試

官網

https://spring.io/projects/spring-data-redis

我們知道常用的Redis客戶端 https://redis.io/clients#java

怎么還有 Spring Data Redis ?

莫慌,小兄弟, 來看個關系圖 幫你捋一捋


Jedis VS Lettuce

在 spring-boot-starter-data-redis 項目 2.X 版本中 ,默認使用 Lettuce 作為 Java Redis 工具庫 , 為啥不用jedis 了?

Why is Lettuce the default Redis client used in Spring Session Redis? #789

也有可能跟jedis 有一段時間不更新了有關系~


Jedis Code

POM依賴

<dependencies><!-- 實現對 Spring Data Redis 的自動化配置 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><!-- 去掉 Lettuce 的依賴, Spring Boot 優先使用 Lettuce 作為 Redis 客戶端 --><exclusion><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId></exclusion></exclusions></dependency><!-- Jedis --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency><!-- 單元測試 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 使用 fastjson 作為 JSON 序列化的工具 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.61</version></dependency><!-- Spring Data Redis 默認使用 Jackson 作為 JSON 序列化的工具 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency></dependencies>

配置文件

spring:# 對應 RedisProperties 類redis:host: 127.0.0.1port: 6379password: # Redis 服務器密碼,默認為空。生產中,一定要設置 Redis 密碼!database: 0 # Redis 數據庫號,默認為 0 。timeout: 0 # Redis 連接超時時間,單位:毫秒。# 對應 RedisProperties.Jedis 內部類jedis:pool:max-active: 8 # 連接池最大連接數,默認為 8 。使用負數表示沒有限制。max-idle: 8 # 默認連接數最小空閑的連接數,默認為 8 。使用負數表示沒有限制。min-idle: 0 # 默認連接池最小空閑的連接數,默認為 0 。允許設置 0 和 正數。max-wait: -1 # 連接池最大阻塞等待時間,單位:毫秒。默認為 -1 ,表示不限制。

配置類

package com.artisan.config;import org.springframework.beans.factory.annotation.Autowired; 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.RedisSerializer;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2021/2/16 21:07* @mark: show me the code , change the world*/@Configuration public class RedisConfiguration {@Autowiredprivate RedisConnectionFactory connectionFactory;public RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String,Object> template = new RedisTemplate();template.setConnectionFactory(connectionFactory);// 序列化工具Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();template.setKeySerializer(stringRedisSerializer);template.setValueSerializer(jackson2JsonRedisSerializer);template.setHashKeySerializer(jackson2JsonRedisSerializer);template.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;} }

單元測試

package com.artisan;import org.junit.Test; import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.junit4.SpringRunner;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2021/2/16 20:56* @mark: show me the code , change the world*/ @RunWith(SpringRunner.class) @SpringBootTest public class JedisTest {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Testpublic void testStringSetKey() {// setstringRedisTemplate.opsForValue().set("artisan", "good1");// getString artisan = stringRedisTemplate.opsForValue().get("artisan");System.out.println(artisan);}}


Lettuce Code

2.x 以上默認 使用的客戶端為Lettuce , 參考 Spring Session - 使用Spring Session從零到一構建分布式session

這里就不贅述了。


Redisson Code

POM依賴

<dependencies><!-- Redisson --><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.11.3</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.61</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency></dependencies>

配置文件

【application.yml】

spring:# 對應 RedisProperties 類redis:host: 127.0.0.1port: 6379 # password: # Redis 服務器密碼,默認為空。生產中,一定要設置 Redis 密碼!database: 0 # Redis 數據庫號,默認為 0 。timeout: 0 # Redis 連接超時時間,單位:毫秒。# 對應 RedissonProperties 類 如果為空 需要注釋掉 # redisson: # config: classpath:redisson.yml # 具體的每個配置項,見 org.redisson.config.Config 類。

使用 Spring Boot 整合 Redisson 時候,通過該配置項,引入一個外部的 Redisson 相關的配置文件 ,引入了 classpath:redisson.yaml 配置文件

引入的 redisson.config 對應的配置文件,對應的類是 org.redisson.config.Config 類。因為示例中,我們使用的比較簡單,所以就沒有做任何 Redisson 相關的自定義配置。 如果沒有配置任何內容,需要在 application.yml 里注釋掉 redisson.config 。

具體配置信息可參考 Spring Boot2.x 整合lettuce redis 和 redisson


配置類

同上

package com.artisan.config;import org.springframework.beans.factory.annotation.Autowired; 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.RedisSerializer;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2021/2/16 21:07* @mark: show me the code , change the world*/@Configuration public class RedisConfiguration {@Autowiredprivate RedisConnectionFactory connectionFactory;public RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String,Object> template = new RedisTemplate();template.setConnectionFactory(connectionFactory);// 序列化工具Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();template.setKeySerializer(stringRedisSerializer);template.setValueSerializer(jackson2JsonRedisSerializer);template.setHashKeySerializer(jackson2JsonRedisSerializer);template.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;} }

單元測試

@Testpublic void test() {stringRedisTemplate.opsForValue().set("uuu", "xxxxx");}

總結

以上是生活随笔為你收集整理的Redis - Spring Data Redis 操作 Jedis 、Lettuce 、 Redisson的全部內容,希望文章能夠幫你解決所遇到的問題。

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