三、SpringBoot2.x整合redis實戰講解
簡介:使用springboot-starter整合reids實戰
一、官網:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-redis
集羣文檔:https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#cluster
二、springboot整合redis相關依賴引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
三、相關配置文件配置
#=========redis基礎配置=========
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6390
# 鏈接超時時間 單位 ms(毫秒)
spring.redis.timeout=3000
#=========redis線程池設置=========
# 鏈接池中的最大空閒鏈接,默認值也是8。
spring.redis.pool.max-idle=200
#鏈接池中的最小空閒鏈接,默認值也是0。
spring.redis.pool.min-idle=200
# 若是賦值爲-1,則表示不限制;pool已經分配了maxActive個jedis實例,則此時pool的狀態爲exhausted(耗盡)。
spring.redis.pool.max-active=2000
# 等待可用鏈接的最大時間,單位毫秒,默認值爲-1,表示永不超時
spring.redis.pool.max-wait=1000
四、常見redistemplate種類講解和緩存實操(使用自動注入)
一、注入模板
@Autowired
private StirngRedisTemplate strTplRedis
二、類型String,List,Hash,Set,ZSet
對應的方法分別是opsForValue()、opsForList()、opsForHash()、opsForSet()、opsForZSet()html
查看某個端口是否被佔用了
建立一個比較基礎的項目
JsonData是一個封裝的響應結果嘞
redis
能夠看官方文檔
https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-redis
點擊Spring Data Redis依賴於這個
集羣部署的參考地址
https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#clusterspring
三、相關配置文件配置
#=========redis基礎配置=========
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6390
# 鏈接超時時間 單位 ms(毫秒)
spring.redis.timeout=3000
#=========redis線程池設置=========
# 鏈接池中的最大空閒鏈接,默認值也是8。
spring.redis.pool.max-idle=200
#鏈接池中的最小空閒鏈接,默認值也是0。
spring.redis.pool.min-idle=200
# 若是賦值爲-1,則表示不限制;pool已經分配了maxActive個jedis實例,則此時pool的狀態爲exhausted(耗盡)。
spring.redis.pool.max-active=2000
# 等待可用鏈接的最大時間,單位毫秒,默認值爲-1,表示永不超時
spring.redis.pool.max-wait=1000
json
RedisTemplate方便咱們去操做Redis。opsForValue就是一個簡單的key value的形式
裏面有不少的方法
jsonData提供了一些返回的值的方法
緩存
這裏尚未配置redis的配置文件。因此用的都是默認值
get從redis獲取key是name的值
在這行加個斷點調試
springboot
複製過來這些配置作一些優化。
spring-boot
默認redis的端口是6379
再次訪問。拿到了值,這就說明配置文件配置成功。
測試