欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot篇】二十一. 基于springboot电商项目 七 springboot整合Redis

程序员文章站 2022-07-14 08:18:08
...

中国加油,武汉加油!

篇幅较长,配合目录观看

案例准备

  1. 本案例基于springboot篇】二十一. 基于springboot电商项目 六 整合RabbitMQ

1. springboot整合Redis

1.1 shop-temp新建springboot-redis

  1. Spring Boot DevTools
  2. Lombok
  3. Spring Data Redis (Access+Driver)

1.2 编写yml

spring:
  redis:
    host: 192.168.59.100
    password: admin

1.3 Test

package com.wpj;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
class SpringbootRedisApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void contextLoads() {
        redisTemplate.opsForValue().set("age", 20);
        System.out.println(redisTemplate.opsForValue().get("age"));
    }
}

1.3 Test第二种

package com.wpj;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
class SpringbootRedisApplicationTests {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void contextLoads2() {
        stringRedisTemplate.opsForValue().set("email", "aaa@qq.com");
        System.out.println(stringRedisTemplate.opsForValue().get("email"));
    }

}

springboot篇】二十一. 基于springboot电商项目 七 springboot整合Redis

2. springboot操纵redis中的事务

@Test
public void testTransaction(){
    redisTemplate.execute(new SessionCallback() {
        @Override
        public Object execute(RedisOperations redisOperations) throws DataAccessException {
            // 开启事务
            redisOperations.multi();
            try{
                // 操作
                redisOperations.opsForValue().set("email","aaa@qq.com");
                // 事务提交
                redisOperations.exec();
            } catch (Exception e) {
                redisOperations.discard();
                e.printStackTrace();
            }
            return null;
        }
    });
}

3. redis设置超时

@Test
public void testTimeout() throws InterruptedException {
    // 添加一个键值对
    stringRedisTemplate.opsForValue().set("email","aaa@qq.com",3, TimeUnit.SECONDS);
    // 给整个键值对设置超时时间 10s
//		stringRedisTemplate.expire("email",10, TimeUnit.SECONDS);

    Thread.sleep(1000);
    Long time1= stringRedisTemplate.getExpire("email");
    System.out.println(time1);

    Thread.sleep(4000);
    Long time2= stringRedisTemplate.getExpire("email");
    System.out.println(time2);
}

springboot篇】二十一. 基于springboot电商项目 七 springboot整合Redis

相关标签: springboot