Gitea Actions Runner Test / test-job (push) Successful in 0s
CI / check (push) Successful in 32s
CI / tests-integration (push) Successful in 1m38s
CI / tests-unit (push) Successful in 1m42s
CI / tests-ui (push) Successful in 2m33s
CI / preflight (push) Skipped
CI / deploy (push) Successful in 2m43s
The in-process cache was a FIFO of 500 entries that was never touched on a read, so a key polled on every request could be evicted by an unrelated burst of dynamic keys. That looked exactly like the cache being cleared at random, and it is what made the site fall back to the database unpredictably. - Evict least-recently-used instead, and raise the default budget to 2000 (CACHE_MEMORY_MAX_ENTRIES). Reading a key now marks it as used, so a hot key only leaves when a hotter one takes its place. - Add opt-in stale-while-revalidate (CachedOptions.staleMs). The grace window lives on the entry, so one call site opting in protects every reader of that key. A failed background refresh keeps serving the last good value instead of falling through to the origin, and is reported once rather than per read. - Invalidate across processes. invalidateKey() now clears memory, deletes the Redis key and publishes a signal, so a value written by one process is no longer served stale by the others for the rest of its TTL. A failed Redis delete no longer skips the broadcast. - Guard against a refresh that started before an invalidation writing its outdated result back into the cache. - Read the news revision at most once a second per process instead of on every call, with a pub/sub signal to drop the local copy when it rotates. A Redis outage now degrades to the in-process cache rather than to no cache at all. - Warm the hot public keys on boot, so the first visitors after a deploy do not each pay for a miss. - Count hits, misses, stale serves, errors and evictions per key, exposed at GET /api/admin/devops/cache. Without it a wrong REDIS_URL, a full budget and a dead origin all look identical from the outside. - Enforce the imaging cache budget for real: records are .img/.json pairs, so the old cap counted files and never removed anything while entries were fresh. Sweeps are throttled per directory and prune to a low-water mark. - Cap the JWT version map, and stop per-test scratch roots from littering the runtime imaging cache. Public read-only endpoints get grace windows; admin, account and auth data deliberately stays fresh. Redis TTLs get a little jitter so keys written together no longer expire together. 3209 tests pass. next build could not be verified on this host: the optimized build is OOM-killed before prerender, so this has not run in a real Next runtime yet.
148 lines
4.6 KiB
TypeScript
148 lines
4.6 KiB
TypeScript
// @ts-nocheck
|
|
import { beforeEach, expect, it, vi } from "vitest";
|
|
|
|
const state = vi.hoisted(() => ({
|
|
revision: "0",
|
|
values: new Map<string, unknown>(),
|
|
get: vi.fn(),
|
|
set: vi.fn(),
|
|
publish: vi.fn(),
|
|
status: "ready",
|
|
// Set by the subscriber mock so a test can simulate another process
|
|
// publishing a new revision.
|
|
deliver: null as ((channel: string, message: string) => void) | null,
|
|
}));
|
|
|
|
vi.mock("@/lib/redis", () => ({
|
|
redis: {
|
|
get: (key: string) => state.get(key),
|
|
set: (key: string, value: string) => state.set(key, value),
|
|
publish: (channel: string, message: string) =>
|
|
state.publish(channel, message),
|
|
get status() {
|
|
return state.status;
|
|
},
|
|
duplicate: () => ({
|
|
on: (event: string, handler: (...args: unknown[]) => void) => {
|
|
if (event === "message") state.deliver = handler;
|
|
},
|
|
subscribe: async () => 1,
|
|
}),
|
|
},
|
|
}));
|
|
vi.mock("@/lib/logger", () => ({
|
|
logger: { error: vi.fn(), warn: vi.fn() },
|
|
}));
|
|
vi.mock("@/lib/cache", () => ({
|
|
cached: async (key: string, _ttl: number, query: () => Promise<unknown>) => {
|
|
if (state.values.has(key)) return state.values.get(key);
|
|
const result = await query();
|
|
state.values.set(key, result);
|
|
return result;
|
|
},
|
|
}));
|
|
|
|
import {
|
|
cacheNews,
|
|
invalidateNewsCache,
|
|
refreshNewsCacheForDelivery,
|
|
} from "./news-cache";
|
|
|
|
beforeEach(() => {
|
|
state.values.clear();
|
|
state.revision = "0";
|
|
state.status = "ready";
|
|
state.get.mockReset().mockImplementation(async () => state.revision);
|
|
state.set
|
|
.mockReset()
|
|
.mockImplementation(async (_key: string, value: string) => {
|
|
state.revision = value;
|
|
});
|
|
state.publish.mockReset().mockResolvedValue(1);
|
|
});
|
|
|
|
it("invalidates a previously cached public list", async () => {
|
|
const query = vi
|
|
.fn()
|
|
.mockResolvedValueOnce([])
|
|
.mockResolvedValueOnce(["new article"]);
|
|
expect(await cacheNews("list", 60000, query)).toEqual([]);
|
|
await invalidateNewsCache();
|
|
expect(await cacheNews("list", 60000, query)).toEqual(["new article"]);
|
|
});
|
|
|
|
it("reads the revision at most once for a burst of reads", async () => {
|
|
// The revision used to be fetched from Redis on every single call, which put
|
|
// a round-trip in front of the very fast path the cache exists to provide.
|
|
const before = state.get.mock.calls.length;
|
|
await cacheNews("burst-a", 60000, async () => ["a"]);
|
|
await cacheNews("burst-b", 60000, async () => ["b"]);
|
|
await cacheNews("burst-c", 60000, async () => ["c"]);
|
|
expect(state.get.mock.calls.length - before).toBeLessThanOrEqual(1);
|
|
});
|
|
|
|
it("picks up a revision published by another process", async () => {
|
|
expect(await cacheNews("list", 60000, async () => ["old"])).toEqual(["old"]);
|
|
|
|
// Another process rotates the revision; the local copy must be dropped so the
|
|
// next read re-reads it instead of serving the previous namespace.
|
|
state.revision = "rev-from-worker";
|
|
state.deliver?.("cache:news-revision", "rev-from-worker");
|
|
|
|
expect(await cacheNews("list", 60000, async () => ["new"])).toEqual(["new"]);
|
|
});
|
|
|
|
it("tells other processes when the revision rotates", async () => {
|
|
await invalidateNewsCache();
|
|
expect(state.publish).toHaveBeenCalledWith(
|
|
"cache:news-revision",
|
|
expect.any(String),
|
|
);
|
|
});
|
|
|
|
it("does not let a stale in-flight read replace a newer revision", async () => {
|
|
let finish!: (value: string[]) => void;
|
|
const old = cacheNews(
|
|
"list",
|
|
60000,
|
|
() =>
|
|
new Promise<string[]>((resolve) => {
|
|
finish = resolve;
|
|
}),
|
|
);
|
|
await vi.waitFor(() => expect(finish).toBeDefined());
|
|
await invalidateNewsCache();
|
|
expect(await cacheNews("list", 60000, async () => ["new"])).toEqual(["new"]);
|
|
finish([]);
|
|
await old;
|
|
expect(await cacheNews("list", 60000, async () => ["wrong"])).toEqual([
|
|
"new",
|
|
]);
|
|
});
|
|
|
|
it("reads fresh data when Redis is unavailable", async () => {
|
|
state.get.mockRejectedValue(Error("offline"));
|
|
expect(await cacheNews("list", 60000, async () => ["fresh"])).toEqual([
|
|
"fresh",
|
|
]);
|
|
});
|
|
|
|
it("still caches in-process when Redis is unavailable", async () => {
|
|
// A Redis outage must not turn every public news read into a database query.
|
|
state.status = "end";
|
|
const query = vi.fn(async () => ["fresh"]);
|
|
expect(await cacheNews("list", 60000, query)).toEqual(["fresh"]);
|
|
expect(await cacheNews("list", 60000, query)).toEqual(["fresh"]);
|
|
expect(query).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("keeps failed durable cache invalidations retryable", async () => {
|
|
state.set.mockRejectedValue(Error("offline"));
|
|
await expect(refreshNewsCacheForDelivery()).rejects.toThrow("offline");
|
|
});
|
|
|
|
it("does not acknowledge an ended Redis connection as refreshed", async () => {
|
|
state.status = "end";
|
|
await expect(refreshNewsCacheForDelivery()).rejects.toThrow();
|
|
});
|