|
@@ -4,10 +4,7 @@ import api.common.pojo.common.ResponseBodyVO;
|
|
import api.common.pojo.param.RedisParameter;
|
|
import api.common.pojo.param.RedisParameter;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.validation.annotation.Validated;
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
@@ -23,33 +20,33 @@ public class RedisController {
|
|
@Resource
|
|
@Resource
|
|
RedisTemplate<String, String> redisTemplate;
|
|
RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
- @RequestMapping("/get")
|
|
|
|
|
|
+ @PostMapping("/get")
|
|
public ResponseBodyVO<String> get(@RequestParam("key") String key) {
|
|
public ResponseBodyVO<String> get(@RequestParam("key") String key) {
|
|
//1 获取 key 对应的值
|
|
//1 获取 key 对应的值
|
|
String value = redisTemplate.opsForValue().get(key);
|
|
String value = redisTemplate.opsForValue().get(key);
|
|
|
|
|
|
- return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS,"请求成功!", value);
|
|
|
|
|
|
+ return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "请求成功!", value);
|
|
}
|
|
}
|
|
|
|
|
|
- @RequestMapping("/set")
|
|
|
|
|
|
+ @PostMapping("/set")
|
|
public ResponseBodyVO<String> set(@RequestBody @Validated RedisParameter redisParameter) {
|
|
public ResponseBodyVO<String> set(@RequestBody @Validated RedisParameter redisParameter) {
|
|
//2 存储键值对并设置时间。
|
|
//2 存储键值对并设置时间。
|
|
- redisTemplate.opsForValue().set(redisParameter.getKey(), redisParameter.getValue(), Duration.ofMinutes(30L));
|
|
|
|
|
|
+ redisTemplate.opsForValue().set(redisParameter.getKey(), redisParameter.getValue(), Duration.ofMinutes(redisParameter.getMinutes()));
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
|
|
}
|
|
}
|
|
|
|
|
|
- @RequestMapping("/getExpire")
|
|
|
|
- public ResponseBodyVO<Long> getExpire(@RequestParam("key") String key) {
|
|
|
|
|
|
+ @PostMapping("/getExpire")
|
|
|
|
+ public ResponseBodyVO<Long> getExpire(@RequestBody RedisParameter redisParameter) {
|
|
//3 获取指定 key 的剩余时间,单位秒。
|
|
//3 获取指定 key 的剩余时间,单位秒。
|
|
- Long time3 = redisTemplate.getExpire("key3");
|
|
|
|
|
|
+ Long time3 = redisTemplate.getExpire(redisParameter.getKey());
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, time3);
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, time3);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- @RequestMapping("/delete")
|
|
|
|
- public ResponseBodyVO<String> delete(@RequestParam("key") String key) {
|
|
|
|
|
|
+ @PostMapping("/delete")
|
|
|
|
+ public ResponseBodyVO<String> delete(@RequestBody RedisParameter redisParameter) {
|
|
//4 根据 key 删除
|
|
//4 根据 key 删除
|
|
- redisTemplate.delete(key);
|
|
|
|
|
|
+ redisTemplate.delete(redisParameter.getKey());
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
|
|
}
|
|
}
|
|
|
|
|