[SpringBoot] Springboot整合redis(一般人都能看懂的Lettuce版本)


作者:java的架構師技術
鏈接:https://zhuanlan.zhihu.com/p/99385034
來源:知乎
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

去年學習的Redis,剛剛學習完就迫不及待的在實戰中用了一下,走了很多坑不過幸好都填上了,需求的不斷變化發現用不上Redis,一開始去掉了,后來想想加進來比較合適。這篇文章主要講解Springboot如何整合開發Redis實現一個基本的案例。使用的是目前Springboot2.x的Lettuce版本。希望對你有幫助。

這里因為不是專門講解Redis的,所以假定你已經學習了Redis,只是希望在SpringBoot2.x中使用。

廢話不多說,直接按照步驟開始,以下的案例均在我自己的電腦上測試成功,如有問題可以聯系我。

一、開發環境

名稱版本Idea2018專業版(已破解)Maven4.0.0SpringBoot2.2.2Redis3.2.1RedisDesktopManager0.8.8jdk1.8

版本的話其實差不不大就沒問題,最主要的就是Springboot的版本,在這里說一下Jedis和Lettuce的區別在哪?

1、Jedis 是直連模式,在多個線程間共享一個 Jedis 實例時是線程不安全的,每個線程都去拿自己的 Jedis 實例,當連接數量增多時,物理連接成本就較高了。

2、Lettuce的連接是基於Netty的,連接實例可以在多個線程間共享,如果你不知道Netty也沒事,大致意思就是一個多線程的應用可以使用同一個連接實例,而不用擔心並發線程的數量。通過異步的方式可以讓我們更好的利用系統資源。

既然有這么大的好處,干脆就用了這個,跟上時代的變化。下面新建一個SpringBootRedis項目,開始整合。

二、整合

步驟一:添加依賴

<!-- spring boot redis 緩存引入 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- lettuce pool 緩存連接池 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>

步驟二:application.properties配置文件

# Redis數據庫索引(默認為0)
spring.redis.database=0  
# Redis服務器地址
spring.redis.host=localhost
# Redis服務器連接端口
spring.redis.port=6379  
# Redis服務器連接密碼(默認為空)
spring.redis.password=
# 連接池最大連接數(使用負值表示沒有限制) 默認 8
spring.redis.lettuce.pool.max-active=8
# 連接池最大阻塞等待時間(使用負值表示沒有限制) 默認 -1
spring.redis.lettuce.pool.max-wait=-1
# 連接池中的最大空閑連接 默認 8
spring.redis.lettuce.pool.max-idle=8
# 連接池中的最小空閑連接 默認 0
spring.redis.lettuce.pool.min-idle=0

當然如果你的屬性文件是yml的,把格式調整一下就OK了。

步驟三:新建config包,創建RedisConfig類

默認情況下RedisTemplate模板只能支持字符串,我們自定義一個RedisTemplate,設置序列化器,這樣我們可以很方便的操作實例對象。

@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) { RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(connectionFactory); return redisTemplate; } }

步驟四:新建bean包,創建User類

public class User implements Serializable { private Integer id; private String userName; private String userSex; public User() { } public User(Integer id, String userName, String userSex) { this.id = id; this.userName = userName; this.userSex = userSex; } //getter和setter方法  //toString方法 }

步驟五:測試插入一個User

@RunWith(SpringRunner.class) @SpringBootTest public class SpringBootredisApplicationTests { @Autowired private RedisTemplate redisTemplate; @Test public void testObj() throws Exception { User user=new User(1, "java的架構師技術棧", "man"); ValueOperations<String, User> operations=redisTemplate.opsForValue(); operations.set("fdd2", user); boolean exists=redisTemplate.hasKey("fdd2"); System.out.println("redis是否存在相應的key"+exists); User getUser = (User)redisTemplate.opsForValue().get("fdd2"); System.out.println("從redis數據庫獲取的user:"+getUser.toString()); } }

在上面這個例子中我們使用redisTemplate調用了opsForValue會得到一個ValueOperations操作。這個是專門操作String類型的數據,所以里面的鍵值對中鍵為String,而值是我們的User。當然redisTemplate還為我們提供了下面幾個。

redisTemplate.opsForValue();//操作字符串 redisTemplate.opsForHash();//操作hash redisTemplate.opsForList();//操作list redisTemplate.opsForSet();//操作set redisTemplate.opsForZSet();//操作有序set

具體使用的時候,你可以根據自己的數據類型選擇相應的方法即可,網上有各種RedisUtil工具類


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM