和其他的NoSQL不同,Redis是存在事務的,儘管他沒有數據庫那麼強大,但是他還是很有用的,尤其是那些需要高併發的網站當中,使用Redis讀/寫數據要比數據庫快得多,如果使用Redis事務在某種場合下去代替數據庫事務,則可以在保證數據一致性的同時,大幅度提高數據讀/寫的響應速度。
爲了保證異性數據的安全性,Redis爲之提供了事務方案。Redis的事務是使用MULTI-EXEC的命令組合,使用它可以提供兩個重要的保證:
- 事務是一個被隔離的操作,事務中的方法都會被Redis進行序列化並按順序執行,事務在執行的過程中不會被其他的客戶端發生的命令所打斷。
- 事務是一個原子性的操作,它要麼全部執行,要麼就什麼都不執行。
在Redis的連接中,我們在 使用Spring中會使用SessionCallback接口進行處理,過程如下:
- 開啓事務
- 命令進入隊列
- 執行事務
先來看看Redis的事務命令:
- multi:開啓事務命令,之後的命令就進入隊列,而不會被馬上執行。
- watch key1 [key2…]:監聽某些鍵,當被監聽的鍵在事務執行前被修改,則事務會回滾,這裏是用了樂觀鎖。
- exec:執行事務,如果被監聽的鍵沒有被修改,則執行命令,否則就回滾命令。
- discard:回滾事務。回滾進入隊列的事務命令,之後就不能再用exec命令提交了。
下面我們來寫一個demo來測試Redis事務。
redisSpring-cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="50" />
<property name="maxTotal" value="100" />
<property name="maxWaitMillis" value="20000" />
</bean>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost" />
<property name="port" value="6379" />
<property name="password" value="123456" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultSerializer" ref="stringRedisSerializer" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="stringRedisSerializer" />
</bean>
</beans>
testRedisTransaction.java
package com.ssm.redis1.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import java.util.List;
public class testRedisTransaction {
public static void main(String[] args){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("redisSpring-cfg.xml");
RedisTemplate redisTemplate=applicationContext.getBean(RedisTemplate.class);
SessionCallback callback=new SessionCallback() {
@Override
public Object execute(RedisOperations redisOperations) throws DataAccessException {
redisOperations.multi();
redisOperations.boundValueOps("key1").set("value1");
String value= (String) redisOperations.boundValueOps("key1").get();
System.out.println("事務執行過程中,命令進入隊列,但是沒有被執行,所以value爲null: value="+value);
//此時list會保存之前進入隊列的所有命令的結果
List list=redisOperations.exec();
//事務結束後,獲取value1
value=(String)redisTemplate.opsForValue().get("key1");
return value;
}
};
//執行Redis命令
String value=(String)redisTemplate.execute(callback);
System.out.println(value);
}
}
運行結果: