SpringBoot 2 x 中的Redis事務

2021-09-13 03:16:34 字數 659 閱讀 3749

在redis中使用事務,通常的命令組合是watch…multi…exec,也就是要在乙個redis連線中執行多個命令,我們可以考慮使用sessioncallback來達成這個目的。

watch:監控redis中的一些鍵

multi:開始事務,但是在開始事務後,不會被立馬執行,而是被存放在乙個佇列裡,也就是說,我們執行一些返回資料的命令,redis也不會馬上執行,返回的資料也是null

exec:執行事務,在執行佇列裡的命令前回去檢查被監控的鍵是否發生過變化,如果發生了變化,則取消事務,如果沒發生,就會執行事務,即要麼全部執行,要麼全部不執行,而且不會被其他客戶端打斷,保證資料的一致性。

如下**清單:

redistemplate.

opsforvalue()

.set

("key1"

,"4");

list execute = redistemplate.

execute

(new

sessioncallback

()})

; system.out.

println

(execute)

;

基於SpringBoot 2 X整合Druid

說明 本文旨在整理springboot 2.x整合druid基礎功能,如有問題請指出 參考資料 基於springboot 2.x版本,這裡是引入druid spring boot2 starter的方式來配置,所以不需要不需要configbean來配置druid,如果引入的是druid,則需要con...

SpringBoot2 x 整合Druid詳細步驟

druid spring boot starter模式 這種方式比較簡單,不需要自己寫配置類。在匯入依賴以及配置好配置檔案yml即可。pom.xml com.alibaba druid spring boot starter 1.1.10 spring datasource url jdbc mys...

springboot2 x基礎 整合redis

在springboot中一般使用redistemplate提供的方法來操作redis。那麼使用springboot整合redis 需要那些步驟呢。環境安裝 任選 centos7 搭建redis 5單機服務 centos7 搭建 redis 5 cluster 集群服務 在專案中新增 spring b...