diff --git a/src/main/java/com/yunhai/bschclassmanage/controller/api/CacheController.java b/src/main/java/com/yunhai/bschclassmanage/controller/api/CacheController.java index aa48737..c9fdc6d 100644 --- a/src/main/java/com/yunhai/bschclassmanage/controller/api/CacheController.java +++ b/src/main/java/com/yunhai/bschclassmanage/controller/api/CacheController.java @@ -7,14 +7,11 @@ 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 org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.concurrent.TimeUnit; /** @@ -96,4 +93,75 @@ public class CacheController extends BaseController { public Object keys(@RequestParam(required = false, defaultValue = "*") String pattern) { return redisUtil.keys(pattern); } + + // ================================ 清除缓存 ================================ + + /** + * 删除指定 key 的缓存(支持多个 key,逗号分隔) + * + * @param key 缓存 key,多个用逗号分隔,如 "a,b,c" + */ + @PostMapping("delete") + public Map delete(@RequestParam String key) { + Map result = new HashMap<>(); + if (StringUtils.isBlank(key)) { + result.put("success", false); + result.put("msg", "key不能为空"); + return result; + } + String[] keys = key.split(","); + long count = redisTemplate.delete(java.util.Arrays.asList(keys)); + result.put("success", true); + result.put("deleted", count); + result.put("msg", "已清除 " + count + " 个 key"); + return result; + } + + /** + * 根据模式匹配批量删除缓存(如 school_class_*) + * 注意:生产大库下 keys 会阻塞,仅在明确范围内使用 + * + * @param pattern 匹配模式,不能为 * + */ + @PostMapping("deletePattern") + public Map deletePattern(@RequestParam String pattern) { + Map result = new HashMap<>(); + if (StringUtils.isBlank(pattern) || "*".equals(pattern.trim())) { + result.put("success", false); + result.put("msg", "禁止用 * 清空全部,请使用 clearAll 接口或指定明确前缀"); + return result; + } + Set keys = redisUtil.keys(pattern); + Map ret = new HashMap<>(); + if (keys == null || keys.isEmpty()) { + result.put("success", true); + result.put("deleted", 0); + result.put("msg", "未匹配到任何 key"); + return result; + } + redisTemplate.delete(keys); + result.put("success", true); + result.put("deleted", keys.size()); + result.put("msg", "已清除 " + keys.size() + " 个 key"); + return result; + } + + /** + * 清空当前 Redis 数据库(仅当前 db,不影响其他 db) + * ⚠️ 高危操作:会删除当前库下所有缓存,请谨慎调用 + */ + @PostMapping("clearAll") + public Map clearAll() { + Map result = new HashMap<>(); + try { + redisTemplate.getConnectionFactory().getConnection().flushDb(); + result.put("success", true); + result.put("msg", "当前数据库已清空"); + } catch (Exception e) { + log.error("清空缓存失败", e); + result.put("success", false); + result.put("msg", "清空失败:" + e.getMessage()); + } + return result; + } }