springboot 整合redis

1.导入pom文件

 <!-- 整合redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2.单元测试

import com.caxs.app.WebApplication;
import org.junit.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.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.concurrent.TimeUnit;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes= WebApplication.class)

/**
 * 图像识别单元测试
 */
public class RedisTest {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Test
    public void setRedis() {
        //缓存中最常用的方法
        redisTemplate.opsForValue().set("first","siwei");
        //设置缓存过期时间为30   单位:秒
        //关于TimeUnit下面有部分源码截图
        redisTemplate.opsForValue().set("second","siweiWu",30, TimeUnit.SECONDS);
        System.out.println("存入缓存成功");
    }

    @Test
    public void setRedis2() {
        //缓存中最常用的方法
        ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();

        stringStringValueOperations.set("hello","1");
        stringStringValueOperations.increment("hello",1);
        System.out.println(stringStringValueOperations.get("hello"));

    }
    @Test
    public void getRedis(){

        String first = redisTemplate.opsForValue().get("first");
        String second = redisTemplate.opsForValue().get("second");

        System.out.println("取出缓存中first的数据是:"+first);
        System.out.println("取出缓存中second的数据是:"+second);

    }
    @Test
    public void delRedis() {
        //根据key删除缓存
        Boolean first1 = redisTemplate.delete("first");

        System.out.println("是否删除成功:"+first1);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值