Redis基本事务流程了解

和其他的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);
    }
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值