RedisController.java 1.5 KB
Newer Older
Q
qinyingjie 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
package com.kwan.springbootkwan.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;

@Api(description = "redis测试信息", tags = "RedisController")
@RestController
@RequestMapping("/test")
public class RedisController {

    public static final String REDIS_KEY = "test:_w";

    @Resource
    private RedisTemplate redisTemplate;


    @ApiOperation(value = "redis添加数据", notes = "redis添加数据")
    @GetMapping("/add")
    public void add() {
        //当前时间时间戳
        String nowStr = String.valueOf(System.currentTimeMillis());
        //设置redis中的key值
        String redisKey = REDIS_KEY.concat(nowStr);
        //向对应的key值添加数据
        redisTemplate.opsForValue().set(REDIS_KEY, "测试向redis中添加数据");
        //设置过期时间
        redisTemplate.expire(REDIS_KEY, 2000000, TimeUnit.SECONDS);
    }

    @ApiOperation(value = "redis获取数据", notes = "redis获取数据")
    @GetMapping("/get")
    public void get() {
        //获取redis中的数据
        String str = (String) redisTemplate.opsForValue().get(REDIS_KEY);
        System.out.println("获取redis中的数据-->" + str);
    }
}