SpringBoot集成大部分插件,步骤基本是一致的。导入依赖,修改配置文件,使用插件。今天来和大家探讨一下SpringBoot集成Redis。

1、导入Redis的起步依赖

<!-- 配置使用redis启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2、配置Redis的连接信息

在 application.properties 配置文件中添加配置 Redis 的连接信息

#Redis
spring.redis.host=127.0.0.1
spring.redis.port=6379

3、编写一个测试方法,进行测试

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netposa.springbootmybatis.domain.User;
import com.netposa.springbootmybatis.mapper.UserMapper;
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.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootmybatisApplication.class)
public class testRedis {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void test() throws JsonProcessingException {

        //从redis缓存中获得指定的数据
        String userListData = redisTemplate.boundValueOps("user.findAll").get();
        //如果redis中没有数据的话
        if(null==userListData){
            //查询数据库获得数据
            List<User> all = userMapper.getAllUser();
            //转换成json格式字符串
            ObjectMapper om = new ObjectMapper();
            userListData = om.writeValueAsString(all);
            //将数据存储到redis中,下次在查询直接从redis中获得数据,不用在查询数据库
            redisTemplate.boundValueOps("user.findAll").set(userListData);
            System.out.println("===============从数据库获得数据===============");
        }else{
            System.out.println("===============从redis缓存中获得数据===============");
        }
        System.out.println(userListData);
    }
}

此测试方法是先去Redis中获取指定的key的数据,如果没有查询到,就去数据库中查询,然后将结果放入Redis中,方便下次查询直接查询Redis。

其中涉及到一些对Redis的操作方法,在此做一个简单的介绍

以下是对指定的一个key进行赋值以及获取值的过程,以及设置某个key的过期时间

String key = "key";
        BoundValueOperations stringTemplate = redisTemplate.boundValueOps(key);
        //赋值key
        stringTemplate.set("test");
        //获取value
        String value = (String) stringTemplate.get();
        //设置超时时间为1天
        stringTemplate.set("testTimeout",1, TimeUnit.DAYS);
        //获取缓存时间,单位 秒
        Long expire = stringTemplate.getExpire();
        System.out.println(key+"的缓存时间为:"+expire);

其他方法如下:

方法名

方法描述

void set(V value)

设定key对应的vlaue值

void set(V value,long offset)

将value值从第offset位开始替换

void set(V value, long timeout, TimeUnit unit)

设置value的超时时间,timeout为数字,unit为单位,例如天,小时等

Boolean setIfAbsent(V value)

判断key是否有对应的value,如果有,则返回false,如果没有,添加,返回true

V get()

返回key对应的value

String get(long start, long end)

从start开始,到end结束,截取value的值

V getAndSet(V value)

替换value的值,并且返回value的旧值

Long increment(long delta)

如果value是数字类型的字符串,那么增加delta,并且返回新的value

Double increment(double delta)

如果value是数字类型的字符串,那么增加delta,并且返回新的value

Integer append(String value)

在value值后面进行添加,并且返回新value的长度

Long size()

返回value的长度

Boolean expire(long var1, TimeUnit var3)

设置key的缓存时间,var1为数字,unit为单位,例如天,小时等,返回是否设置成功

Boolean expireAt(Date var1)

设置key的具体到期时间,并且返回是否设置成功

Long getExpire()

返回key的剩余缓存时间,单位:秒

K getKey()

返回key的名称

DataType getType()

获取key的类型

Boolean persist()

删除key的缓存时间

void rename(K var1)

修改key的名称