From b72976bd6e5cbbb1c629517c067247f21af343d4 Mon Sep 17 00:00:00 2001 From: wanghu Date: Mon, 17 Aug 2026 09:31:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=93=E5=AD=98=E6=9F=A5?= =?UTF-8?q?=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/api/CacheController.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/main/java/com/yunhai/bschclassmanage/controller/api/CacheController.java diff --git a/src/main/java/com/yunhai/bschclassmanage/controller/api/CacheController.java b/src/main/java/com/yunhai/bschclassmanage/controller/api/CacheController.java new file mode 100644 index 0000000..aa48737 --- /dev/null +++ b/src/main/java/com/yunhai/bschclassmanage/controller/api/CacheController.java @@ -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 getByKey(@RequestParam String key) { + Map 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); + } +}