feat(redis): add local expiration modes

This commit is contained in:
onenewcode authored and GitHub committed 2026-07-26 23:33:56 +08:00
1 parent 3a2dc4225a
commit 51fa058edb
35 files changed
+2918 -98

No files matched your search

@@ -0,0 +1,99 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
invoke: vi.fn(),
}));
vi.mock("@tauri-apps/api/core", () => ({
invoke: mocks.invoke,
}));
vi.mock("@tauri-apps/api/event", () => ({
listen: vi.fn(),
}));
describe("Redis expiry Tauri API", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.unstubAllGlobals();
});
it("invokes EXPIREAT with the Unix timestamp", async () => {
mocks.invoke.mockResolvedValue(undefined);
const { redisSetExpireAt } = await import("@/lib/backend/tauri");
await redisSetExpireAt("redis-1", 2, "c2Vzc2lvbg==", 1_735_689_600);
expect(mocks.invoke).toHaveBeenCalledWith("redis_set_expire_at", {
connectionId: "redis-1",
db: 2,
keyRaw: "c2Vzc2lvbg==",
expireAt: 1_735_689_600,
});
});
it("forwards relative TTL and persist values unchanged", async () => {
mocks.invoke.mockResolvedValue(undefined);
const { redisSetTtl } = await import("@/lib/backend/tauri");
await redisSetTtl("redis-1", 2, "c2Vzc2lvbg==", 90);
await redisSetTtl("redis-1", 2, "c2Vzc2lvbg==", -1);
expect(mocks.invoke).toHaveBeenNthCalledWith(1, "redis_set_ttl", {
connectionId: "redis-1",
db: 2,
keyRaw: "c2Vzc2lvbg==",
ttl: 90,
});
expect(mocks.invoke).toHaveBeenNthCalledWith(2, "redis_set_ttl", {
connectionId: "redis-1",
db: 2,
keyRaw: "c2Vzc2lvbg==",
ttl: -1,
});
});
});
describe("Redis expiry HTTP API", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.unstubAllGlobals();
});
function stubFetch() {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue(undefined),
});
vi.stubGlobal("fetch", fetchMock);
return fetchMock;
}
function lastCall(fetchMock: ReturnType<typeof stubFetch>): { url: string; body: Record<string, unknown> } {
const [url, init] = fetchMock.mock.calls.at(-1) as [string, RequestInit];
return { url, body: JSON.parse(String(init.body)) as Record<string, unknown> };
}
it("posts relative TTL, persist, and absolute expiration requests", async () => {
const fetchMock = stubFetch();
const { redisSetExpireAt, redisSetTtl } = await import("@/lib/backend/http");
await redisSetTtl("redis-1", 2, "c2Vzc2lvbg==", 90);
expect(lastCall(fetchMock)).toEqual({
url: "/api/redis/set-ttl",
body: { connectionId: "redis-1", db: 2, keyRaw: "c2Vzc2lvbg==", ttl: 90 },
});
await redisSetTtl("redis-1", 2, "c2Vzc2lvbg==", -1);
expect(lastCall(fetchMock)).toEqual({
url: "/api/redis/set-ttl",
body: { connectionId: "redis-1", db: 2, keyRaw: "c2Vzc2lvbg==", ttl: -1 },
});
await redisSetExpireAt("redis-1", 2, "c2Vzc2lvbg==", 1_735_689_600);
expect(lastCall(fetchMock)).toEqual({
url: "/api/redis/set-expire-at",
body: { connectionId: "redis-1", db: 2, keyRaw: "c2Vzc2lvbg==", expireAt: 1_735_689_600 },
});
});
});
+1
View File
@@ -395,6 +395,7 @@ export const redisStreamAdd = forward("redisStreamAdd");
export const redisJsonSet = forward("redisJsonSet");
export const redisCheckJsonModule = forward("redisCheckJsonModule");
export const redisSetTtl = forward("redisSetTtl");
export const redisSetExpireAt = forward("redisSetExpireAt");
export const redisDeleteKeys = forward("redisDeleteKeys");
export const redisFlushDb = forward("redisFlushDb");
export const redisExecuteCommand = forward("redisExecuteCommand");
+4
View File
@@ -2012,6 +2012,10 @@ export async function redisSetTtl(connectionId: string, db: number, keyRaw: stri
return post("/api/redis/set-ttl", { connectionId, db, keyRaw, ttl });
}
export async function redisSetExpireAt(connectionId: string, db: number, keyRaw: string, expireAt: number): Promise<void> {
return post("/api/redis/set-expire-at", { connectionId, db, keyRaw, expireAt });
}
export async function redisDeleteKeys(connectionId: string, db: number, keyRaws: string[]): Promise<number> {
return post("/api/redis/delete-keys", { connectionId, db, keyRaws });
}
+4
View File
@@ -1731,6 +1731,10 @@ export async function redisSetTtl(connectionId: string, db: number, keyRaw: stri
return invoke("redis_set_ttl", { connectionId, db, keyRaw, ttl });
}
export async function redisSetExpireAt(connectionId: string, db: number, keyRaw: string, expireAt: number): Promise<void> {
return invoke("redis_set_expire_at", { connectionId, db, keyRaw, expireAt });
}
export async function redisDeleteKeys(connectionId: string, db: number, keyRaws: string[]): Promise<number> {
return invoke("redis_delete_keys", { connectionId, db, keyRaws });
}