添加缓存查看
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package com.yunhai.bschclassmanage.controller.api;
|
||||
|
||||
import com.yunhai.bschclassmanage.config.Config;
|
||||
import com.yunhai.bschclassmanage.controller.BaseController;
|
||||
import com.yunhai.bschclassmanage.utils.RedisUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 缓存查看接口
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping(Config.API_PATH + "/cache/")
|
||||
@CrossOrigin(originPatterns = {"*"}, allowCredentials = "true")
|
||||
public class CacheController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
/**
|
||||
* 根据 key 查看缓存内容
|
||||
* 返回 key 是否存在、数据类型、剩余过期时间(秒)以及对应的值
|
||||
* 支持 string / hash / list / set / zset 五种类型
|
||||
*
|
||||
* @param key 缓存 key
|
||||
*/
|
||||
@GetMapping("get")
|
||||
public Map<String, Object> getByKey(@RequestParam String key) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("key", key);
|
||||
|
||||
if (StringUtils.isBlank(key)) {
|
||||
result.put("exists", false);
|
||||
result.put("msg", "key不能为空");
|
||||
return result;
|
||||
}
|
||||
|
||||
Boolean exists = redisTemplate.hasKey(key);
|
||||
if (exists == null || !exists) {
|
||||
result.put("exists", false);
|
||||
result.put("msg", "缓存不存在或已过期");
|
||||
return result;
|
||||
}
|
||||
|
||||
result.put("exists", true);
|
||||
String type = redisTemplate.type(key).name().toLowerCase();
|
||||
result.put("type", type);
|
||||
result.put("expire", redisTemplate.getExpire(key, TimeUnit.SECONDS)); // -1 永不过期,-2 不存在
|
||||
|
||||
Object value;
|
||||
switch (type) {
|
||||
case "string":
|
||||
value = redisTemplate.opsForValue().get(key);
|
||||
break;
|
||||
case "hash":
|
||||
value = redisTemplate.opsForHash().entries(key);
|
||||
break;
|
||||
case "list":
|
||||
value = redisTemplate.opsForList().range(key, 0, -1);
|
||||
break;
|
||||
case "set":
|
||||
value = redisTemplate.opsForSet().members(key);
|
||||
break;
|
||||
case "zset":
|
||||
value = redisTemplate.opsForZSet().rangeWithScores(key, 0, -1);
|
||||
break;
|
||||
default:
|
||||
value = null;
|
||||
break;
|
||||
}
|
||||
result.put("value", value);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据模式匹配查询 key 列表(如 school_class_*),方便先找到 key 再查看内容
|
||||
*
|
||||
* @param pattern 匹配模式,默认 * 表示所有 key
|
||||
*/
|
||||
@GetMapping("keys")
|
||||
public Object keys(@RequestParam(required = false, defaultValue = "*") String pattern) {
|
||||
return redisUtil.keys(pattern);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user