feat(cache): 通用缓存按区统计与清空接口
GET /cache/regions 列出 kv_cache 各区的未过期条数、已过期条数、占用字节、最近写入;
POST /cache/regions/{region}/clear 清空一个区(admin)。contracts 加区名中文映射
cacheRegionLabel,web 加对应 hooks。
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Cxwgnn2pbzQ7rXv3rq7fei
This commit is contained in:
1 parent
ad3bf8926b
commit
b7460d5d0a
5 files changed
+190
-6
No files matched your search
@@ -1,10 +1,14 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import type { CacheClearResult, ImageCacheStatus, TmdbCacheList } from "@umt/contracts";
|
||||
import type { CacheClearResult, CacheRegionList, ImageCacheStatus, TmdbCacheList } from "@umt/contracts";
|
||||
import { apiData } from "~/lib/api.ts";
|
||||
|
||||
// C2-A 缓存管理:TMDB 缓存(条数 / 上限 / TTL / 清空)、图片代理缓存(用量 / 清空)
|
||||
// C2-A 缓存管理:TMDB 缓存(条数 / 上限 / TTL / 清空)、图片代理缓存(用量 / 清空)、通用缓存按区统计与清空
|
||||
|
||||
const keys = { tmdb: ["cache", "tmdb"] as const, images: ["cache", "images"] as const };
|
||||
const keys = {
|
||||
tmdb: ["cache", "tmdb"] as const,
|
||||
images: ["cache", "images"] as const,
|
||||
regions: ["cache", "regions"] as const,
|
||||
};
|
||||
|
||||
export function useTmdbCache() {
|
||||
return useQuery({
|
||||
@@ -17,7 +21,7 @@ export function useClearTmdbCache() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: () => apiData<CacheClearResult>("/cache/tmdb/clear", { method: "POST" }),
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: keys.tmdb }),
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["cache"] }),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -35,3 +39,20 @@ export function useClearImageCache() {
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: keys.images }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCacheRegions() {
|
||||
return useQuery({
|
||||
queryKey: keys.regions,
|
||||
queryFn: ({ signal }) => apiData<CacheRegionList>("/cache/regions", { signal }),
|
||||
});
|
||||
}
|
||||
|
||||
/** 清空一个区;TMDB 两个区也在这里清,顺带刷新 TMDB 缓存计数 */
|
||||
export function useClearCacheRegion() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (region: string) =>
|
||||
apiData<CacheClearResult>(`/cache/regions/${encodeURIComponent(region)}/clear`, { method: "POST" }),
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["cache"] }),
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user