• Spring Boot2.0之 整合Redis事务


    Redis事物

    Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证:

    事务是一个单独的隔离操作:事务中的所有命令都会序列化、按顺序地执行。事务在执行的过程中,不会被其他客户端发送来的命令请求所打断。

    事务是一个原子操作:事务中的命令要么全部被执行,要么全部都不执行。

    一个事务从开始到执行会经历以下三个阶段:

    开始事务。

    命令入队。

    执行事务。

    事务的开启很简单的:

    可以使用AOP封装下~

     跟之前使用MySQL数据库没啥子区别的

    看下目录

     

    pom:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>redis</groupId>
      <artifactId>com.toov5.redis</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.0.0.RELEASE</version>
    	</parent>
    	<dependencies>
    		<!-- SpringBoot web 核心组件 -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-data-redis</artifactId>
    		</dependency>
    	</dependencies>
    
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<configuration>
    					<source>1.8</source>
    					<target>1.8</target>
    				</configuration>
    			</plugin>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    				<configuration>
    					<maimClass>
    						com.itmayiedu.controller.IndexController</maimClass>
    				</configuration>
    				<executions>
    					<execution>
    						<goals>
    							<goal>repackage</goal>
    						</goals>
    					</execution>
    				</executions>
    
    			</plugin>
    		</plugins>
    	</build>
      
      
    </project>
    

     service:

    package com.toov5.service;
    
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Component;
    
    @Component
    public class RedisService {
    
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
        //这样该方法支持多种数据类型 
        public void set(String key , Object object, Long time){
            //开启事务权限
            stringRedisTemplate.setEnableTransactionSupport(true);
            try {
                //开启事务
                stringRedisTemplate.multi();
                
                String argString =(String)object;  //强转下
                stringRedisTemplate.opsForValue().set(key, argString);
                
                //成功就提交
                stringRedisTemplate.exec();
            } catch (Exception e) {
                //失败了就回滚
                stringRedisTemplate.discard();
                
            }
            if (object instanceof String ) {  //判断下是String类型不
                String argString =(String)object;  //强转下
                //存放String类型的
                stringRedisTemplate.opsForValue().set(key, argString);
            }
            //如果存放Set类型
            if (object instanceof Set) {
                Set<String> valueSet =(Set<String>)object;
                for(String string:valueSet){
                    stringRedisTemplate.opsForSet().add(key, string);  //此处点击下源码看下 第二个参数可以放好多
                }
            }
            //设置有效期
            if (time != null) {
                stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            
        }
        //做个封装
        public void setString(String key, Object object){
            String argString =(String)object;  //强转下
            //存放String类型的
            stringRedisTemplate.opsForValue().set(key, argString);
        }
        public void setSet(String key, Object object){
            Set<String> valueSet =(Set<String>)object;
            for(String string:valueSet){
                stringRedisTemplate.opsForSet().add(key, string);  //此处点击下源码看下 第二个参数可以放好多
            }
        }
        
        public String getString(String key){
         return    stringRedisTemplate.opsForValue().get(key);
        }
        
        
    }
    package com.toov5.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.toov5.service.RedisService;
    
    @RestController
    public class IndexController {
      @Autowired
      private RedisService redisService;
        
       @RequestMapping("/setString")
       public String setString(String key, String value){
           redisService.set(key, value, 5000l); //超时时间5000s   l表示long型
           return "成功";
       }
       
       @RequestMapping("get")
       public String get(String key){
           return redisService.getString(key);
       }
    }
    package com.toov5.app;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication(scanBasePackages={"com.toov5.*"})
    public class app {
     public static void main(String[] args) {
        SpringApplication.run(app.class, args);
    }
    }

    yml: 注意一定要是Master啊 Slave不支持写哦

    spring:
      redis:
        database: 0    
        host:  192.168.91.3
        port:  6379
        password:  123
        jedis:
          pool:
            max-active: 8
            max-wait: -1
            max-idle: 8
            min-idle: 0
        timeout: 10000
    

      

    运行结果:

    Redis

  • 相关阅读:
    POJ1125 Stockbroker Grapevine(spfa枚举)
    20160402javaweb 开发模式
    20160331javaweb之JSP 标签技术
    20160331javaweb之JSP include 指令&&九大隐式对象
    20160330javaweb之session 小练习
    20160329javaweb之JSP -session入门
    20160329javaweb之JSP -cookie入门
    20160328 javaweb Cookie 小练习
    20160327javaweb 之JSP入门
    HttpServlet was not found on the Java
  • 原文地址:https://www.cnblogs.com/toov5/p/9890724.html
Copyright © 2020-2023  润新知