RedisController.java 1.5 KB
Newer Older
Q
qinyingjie 已提交
1 2
package com.kwan.springbootkwan.controller;

3
import com.kwan.springbootkwan.entity.Result;
Q
qinyingjie 已提交
4 5
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
6
import lombok.extern.slf4j.Slf4j;
Q
qinyingjie 已提交
7 8 9 10 11 12 13 14
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

15 16 17
import static com.kwan.springbootkwan.constant.CommonConstant.PREFIX_REDIS_KEY;

@Slf4j
18
@Api(value = "redis测试信息", tags = "RedisController")
Q
qinyingjie 已提交
19 20 21 22 23 24 25 26 27
@RestController
@RequestMapping("/test")
public class RedisController {

    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "redis添加数据", notes = "redis添加数据")
    @GetMapping("/add")
28
    public Result add(String key, String value) {
Q
qinyingjie 已提交
29
        //向对应的key值添加数据
30
        redisTemplate.opsForValue().set(PREFIX_REDIS_KEY + key, value);
Q
qinyingjie 已提交
31
        //设置过期时间
32 33
        redisTemplate.expire(PREFIX_REDIS_KEY, 2000000, TimeUnit.SECONDS);
        return Result.ok();
Q
qinyingjie 已提交
34 35 36 37
    }

    @ApiOperation(value = "redis获取数据", notes = "redis获取数据")
    @GetMapping("/get")
38
    public Result get(String key) {
Q
qinyingjie 已提交
39
        //获取redis中的数据
40 41 42
        String value = (String) redisTemplate.opsForValue().get(PREFIX_REDIS_KEY + key);
        log.info("获取redis中的数据-->{}", value);
        return Result.ok(value);
Q
qinyingjie 已提交
43 44
    }
}