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

中国加油,武汉加油!

篇幅较长,配合目录观看

案例准备

  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", "[email protected]");
        System.out.println(stringRedisTemplate.opsForValue().get("email"));
    }

}

在这里插入图片描述

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","[email protected]");
                // 事务提交
                redisOperations.exec();
            } catch (Exception e) {
                redisOperations.discard();
                e.printStackTrace();
            }
            return null;
        }
    });
}

3. redis设置超时

@Test
public void testTimeout() throws InterruptedException {
    // 添加一个键值对
    stringRedisTemplate.opsForValue().set("email","[email protected]",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);
}

在这里插入图片描述

发布了126 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/TheNew_One/article/details/105228493