feat(redis): poll full values and track TTL countdowns

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-07-31 23:11:48 +08:00
1 parent a6a072294a
commit c69f1352d4
7 files changed
+268 -169

No files matched your search

@@ -101,7 +101,7 @@ describe("Redis expiry mode wiring", () => {
const currentTtl = findFunction(viewerSource, "currentEditableTtl").getText();
expect(start).toContain("redisExpiryModeForTtl(ttl)");
expect(currentTtl).toContain("computeTtlForExpiryEdit(autoRefreshEnabled.value, countdownTtl.value, data.value.ttl)");
expect(currentTtl).toContain("computeTtlForExpiryEdit(countdownTtl.value, data.value.ttl)");
expect(viewerSource).toContain("unixSecondsToCalendarDateTime(Math.ceil(Date.now() / 1_000) + ttl)");
expect(save).toContain("validateRedisExpiry");
expect(save).toContain("applyRedisExpiryPolicy");
@@ -234,7 +234,7 @@ describe("native RedisJSON editor", () => {
});
it("keeps retained drafts out of background refresh and exposes word wrap for every JSON editor", () => {
const refreshTtl = findFunction("refreshTtl");
const refreshAutoValue = findFunction("refreshAutoValue");
const hashSearch = findFunction("onHashSearch");
const viewMember = findFunction("viewMember");
const setMemberValueFormat = findFunction("setMemberValueFormat");
@@ -243,9 +243,9 @@ describe("native RedisJSON editor", () => {
const labels = templateElements(parsedViewer.descriptor.template!.ast as unknown as TemplateElement);
const stringTextarea = findTemplateElement((element) => element.tag === "textarea" && directiveExpression(element, "model") === "editValue");
const memberTextarea = findTemplateElement((element) => element.tag === "textarea" && directiveExpression(element, "model") === "memberEditValue");
const refreshItem = findTemplateElement((element) => element.tag === "DropdownMenuItem" && directiveExpression(element, "on", "select") === "refreshValueAndStreamGroups");
const refreshButton = findTemplateElement((element) => element.tag === "Button" && element.props.some((prop) => prop.type === 6 && prop.name === "data-redis-value-refresh"));
expect(refreshTtl.getText().match(/hasUnsavedRedisDraft\.value/g)).toHaveLength(2);
expect(refreshAutoValue.getText().match(/hasUnsavedRedisDraft\.value/g)).toHaveLength(2);
expect(hashSearch.getText()).toContain("if (!hasRetainedMemberDraft.value) clearSelectedMember();");
expect(viewMember.getText()).toContain("hasRetainedMemberDraft.value");
// Clean JSON → other format must clear memberDraftFormat so rawText is not compared to the pretty baseline.
@@ -258,6 +258,6 @@ describe("native RedisJSON editor", () => {
expect(labels.some((element) => element.tag === "label" && directiveExpression(element, "if") === "isTextRedisFormat(memberValueView)")).toBe(true);
expect(directiveExpression(stringTextarea, "bind", "readonly")).toBe("!canEditCurrentStringFormat || savingString");
expect(directiveExpression(memberTextarea, "bind", "readonly")).toBe("savingMember");
expect(directiveExpression(refreshItem, "bind", "disabled")).toBe("hasUnsavedRedisDraft");
expect(directiveExpression(refreshButton, "bind", "disabled")).toBe("loading || refreshingValue || hasUnsavedRedisDraft");
});
});
+30 -25
View File
@@ -1,22 +1,28 @@
import { describe, expect, it } from "vitest";
import { computeAutoRefreshTick, computeDisplayTtl, computeTtlForExpiryEdit, DEFAULT_REDIS_AUTO_REFRESH_INTERVAL_SECONDS, normalizeRedisAutoRefreshInterval } from "@/lib/redis/redisAutoRefresh";
describe("computeAutoRefreshTick", () => {
it("returns idle when auto-refresh is disabled", () => {
expect(computeAutoRefreshTick(false, 10)).toEqual({ type: "idle" });
expect(computeAutoRefreshTick(false, 0)).toEqual({ type: "idle" });
expect(computeAutoRefreshTick(false, 5)).toEqual({ type: "idle" });
});
import { computeDisplayTtl, computeTtlCountdownTick, computeTtlCountdownValue, computeTtlForExpiryEdit, DEFAULT_REDIS_AUTO_REFRESH_INTERVAL_SECONDS, normalizeRedisAutoRefreshInterval } from "@/lib/redis/redisAutoRefresh";
describe("computeTtlCountdownTick", () => {
it("returns decrement while a positive TTL remains", () => {
expect(computeAutoRefreshTick(true, 10)).toEqual({ type: "decrement" });
expect(computeAutoRefreshTick(true, 1)).toEqual({ type: "decrement" });
expect(computeTtlCountdownTick(10)).toEqual({ type: "decrement" });
expect(computeTtlCountdownTick(1)).toEqual({ type: "decrement" });
});
it("stops decrementing after the TTL has reached zero", () => {
expect(computeAutoRefreshTick(true, 0)).toEqual({ type: "idle" });
expect(computeAutoRefreshTick(true, -1)).toEqual({ type: "idle" });
expect(computeTtlCountdownTick(0)).toEqual({ type: "idle" });
expect(computeTtlCountdownTick(-1)).toEqual({ type: "idle" });
});
});
describe("computeTtlCountdownValue", () => {
it("accounts for time elapsed while countdown timers are paused", () => {
expect(computeTtlCountdownValue(60, 10_000, 40_000)).toBe(30);
});
it("preserves Redis sentinel values and clamps expired TTLs", () => {
expect(computeTtlCountdownValue(5, 10_000, 20_000)).toBe(0);
expect(computeTtlCountdownValue(-1, 10_000, 20_000)).toBe(-1);
expect(computeTtlCountdownValue(-2, 10_000, 20_000)).toBe(-2);
});
});
@@ -30,34 +36,33 @@ describe("normalizeRedisAutoRefreshInterval", () => {
});
describe("computeDisplayTtl", () => {
it("returns server TTL when auto-refresh is disabled", () => {
expect(computeDisplayTtl(false, 3, 10)).toBe(10);
it("returns the live countdown regardless of automatic network refresh", () => {
expect(computeDisplayTtl(3, 10)).toBe(3);
});
it("does not flash back to the stale server TTL at zero", () => {
expect(computeDisplayTtl(true, 0, 5)).toBe(0);
expect(computeDisplayTtl(0, 5)).toBe(0);
});
it("returns live countdown when auto-refresh is active and counting", () => {
expect(computeDisplayTtl(true, 5, 10)).toBe(5);
expect(computeDisplayTtl(true, 1, 10)).toBe(1);
it("returns live countdown while counting", () => {
expect(computeDisplayTtl(5, 10)).toBe(5);
expect(computeDisplayTtl(1, 10)).toBe(1);
});
it("clamps an active countdown below zero instead of showing stale data", () => {
expect(computeDisplayTtl(true, -1, 10)).toBe(0);
expect(computeDisplayTtl(-1, 10)).toBe(0);
});
});
describe("computeTtlForExpiryEdit", () => {
it("keeps a last-confirmed positive TTL during the in-flight zero-countdown window", () => {
expect(computeTtlForExpiryEdit(true, 0, 5)).toBe(5);
expect(computeTtlForExpiryEdit(true, -1, 5)).toBe(5);
expect(computeTtlForExpiryEdit(0, 5)).toBe(5);
expect(computeTtlForExpiryEdit(-1, 5)).toBe(5);
});
it("uses the live positive countdown and preserves non-expiring server states", () => {
expect(computeTtlForExpiryEdit(true, 3, 10)).toBe(3);
expect(computeTtlForExpiryEdit(false, 3, 10)).toBe(10);
expect(computeTtlForExpiryEdit(true, 0, -1)).toBe(-1);
expect(computeTtlForExpiryEdit(true, 0, -2)).toBe(-2);
expect(computeTtlForExpiryEdit(3, 10)).toBe(3);
expect(computeTtlForExpiryEdit(0, -1)).toBe(-1);
expect(computeTtlForExpiryEdit(0, -2)).toBe(-2);
});
});
+15 -9
View File
@@ -10,7 +10,7 @@ export const MAX_REDIS_AUTO_REFRESH_INTERVAL_SECONDS = 3600;
export const DEFAULT_REDIS_AUTO_REFRESH_INTERVAL_SECONDS = 5;
/** Action computed after evaluating one visible TTL countdown tick. */
export type AutoRefreshTickAction = { type: "idle" } | { type: "decrement" };
export type TtlCountdownTickAction = { type: "idle" } | { type: "decrement" };
/** Keep a user-entered polling frequency within a safe, whole-second range. */
export function normalizeRedisAutoRefreshInterval(value: unknown): number {
@@ -22,22 +22,28 @@ export function normalizeRedisAutoRefreshInterval(value: unknown): number {
/**
* Evaluate one one-second visible TTL countdown tick.
*
* @param enabled Whether auto-refresh is currently toggled on.
* @param countdownTtl Current countdown value in seconds.
* @returns The action the caller should take this tick.
*/
export function computeAutoRefreshTick(enabled: boolean, countdownTtl: number): AutoRefreshTickAction {
return enabled && countdownTtl > 0 ? { type: "decrement" } : { type: "idle" };
export function computeTtlCountdownTick(countdownTtl: number): TtlCountdownTickAction {
return countdownTtl > 0 ? { type: "decrement" } : { type: "idle" };
}
/** Compute a TTL countdown from the time the server value was observed. */
export function computeTtlCountdownValue(serverTtl: number, observedAtMs: number, nowMs: number): number {
if (serverTtl <= 0) return serverTtl;
const elapsedSeconds = Math.max(0, Math.floor((nowMs - observedAtMs) / 1000));
return Math.max(serverTtl - elapsedSeconds, 0);
}
/**
* Compute the TTL value that should be displayed in the badge.
*
* When auto-refresh is active, the live countdown value is shown. The
* The local countdown is independent from automatic network refreshes, so the
* last known server TTL must not reappear after the countdown reaches zero.
*/
export function computeDisplayTtl(autoRefreshEnabled: boolean, countdownTtl: number, serverTtl: number): number {
return autoRefreshEnabled ? Math.max(countdownTtl, 0) : serverTtl;
export function computeDisplayTtl(countdownTtl: number, serverTtl: number): number {
return serverTtl > 0 ? Math.max(countdownTtl, 0) : serverTtl;
}
/**
@@ -47,8 +53,8 @@ export function computeDisplayTtl(autoRefreshEnabled: boolean, countdownTtl: num
* is still in flight. Do not turn a last-confirmed positive Redis TTL into
* PERSIST during that window.
*/
export function computeTtlForExpiryEdit(autoRefreshEnabled: boolean, countdownTtl: number, serverTtl: number): number {
export function computeTtlForExpiryEdit(countdownTtl: number, serverTtl: number): number {
if (serverTtl <= 0) return serverTtl;
const displayedTtl = computeDisplayTtl(autoRefreshEnabled, countdownTtl, serverTtl);
const displayedTtl = computeDisplayTtl(countdownTtl, serverTtl);
return displayedTtl > 0 ? displayedTtl : serverTtl;
}