SpringBoot整合Lettuce redis過程解析
首先解釋一下Lettuce客戶端:
Lettuce 和 Jedis 的都是連接Redis Server的客戶端程序。Jedis在實現(xiàn)上是直連redis server,多線程環(huán)境下非線程安全,除非使用連接池,為每個Jedis實例增加物理連接。Lettuce基于Netty的連接實例(StatefulRedisConnection),可以在多個線程間并發(fā)訪問,且線程安全,滿足多線程環(huán)境下的并發(fā)訪問,同時它是可伸縮的設(shè)計,一個連接實例不夠的情況也可以按需增加連接實例。
1、添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2、添加redis配置
spring:
redis:
host: ****
password:****
port: 6379
# 連接超時時間(毫秒)
timeout: 1000
# Redis默認(rèn)情況下有16個分片,這里配置具體使用的分片,默認(rèn)是0
database: 0
# 連接池配置
lettuce:
pool:
# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制) 默認(rèn) 8
max-active: 8
# 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制) 默認(rèn) -1
max-wait: -1
# 連接池中的最大空閑連接 默認(rèn) 8
max-idle: 8
# 連接池中的最小空閑連接 默認(rèn) 0
min-idle: 0
3、實現(xiàn)邏輯
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public String testRedis(){
ExecutorService executorService = Executors.newFixedThreadPool(1000);
IntStream.range(0, 1000).forEach(i -> executorService.execute(() -> stringRedisTemplate.opsForValue().increment("lcl",1)));
System.out.println("lcl1=============" + stringRedisTemplate.opsForValue().get("lcl"));
stringRedisTemplate.opsForValue().set("lcl1","val1");
String val1 = stringRedisTemplate.opsForValue().get("lcl1");
System.out.println("lcl1=============" + val1);
String key = "redis:test:demo1";
User user = new User();
user.setId(100L);
user.setUsername("u2");
user.setPassword("p2");
stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(user));
String valUser = stringRedisTemplate.opsForValue().get(key);
System.out.println("redis:test:demo1=============" + valUser);
User getUser = JSON.parseObject(valUser, User.class);
System.out.println("redis:test:demo1=============" + getUser.getUsername()+ "========" + getUser.getPassword());
return null;
}
測試結(jié)果:


由于redis有String、list、set、zset、hash、geo等類型,因此使用時不止使用opsForValue()方法,具體的對應(yīng)方法如下:
- opsForValue: 對應(yīng) String(字符串)
- opsForZSet: 對應(yīng) ZSet(有序集合)
- opsForHash: 對應(yīng) Hash(哈希)
- opsForList: 對應(yīng) List(列表)
- opsForSet: 對應(yīng) Set(集合)
- opsForGeo: 對應(yīng) GEO(地理位置)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用Calendar類實現(xiàn)動態(tài)日歷
這篇文章主要為大家詳細(xì)介紹了Java使用Calendar類實現(xiàn)動態(tài)日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
Java并發(fā)編程中的ConcurrentLinkedQueue詳解
這篇文章主要介紹了Java并發(fā)編程中的ConcurrentLinkedQueue詳解,GetThread線程不會因為ConcurrentLinkedQueue隊列為空而等待,而是直接返回null,所以當(dāng)實現(xiàn)隊列不空時,等待時,則需要用戶自己實現(xiàn)等待邏輯,需要的朋友可以參考下2023-12-12
Spring Boot中使用activiti的方法教程(一)
最近一直研究springboot,下面這篇文章主要給大家介紹了關(guān)于Spring Boot中使用activiti的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Java實現(xiàn)過濾掉map集合中key或value為空的值示例
這篇文章主要介紹了Java實現(xiàn)過濾掉map集合中key或value為空的值,涉及java針對map的簡單遍歷、判斷、移除等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
解決HttpServletRequest 流數(shù)據(jù)不可重復(fù)讀的操作
這篇文章主要介紹了解決HttpServletRequest 流數(shù)據(jù)不可重復(fù)讀的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

