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); } }