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.
133 lines
4.3 KiB
TypeScript
133 lines
4.3 KiB
TypeScript
// @ts-nocheck
|
|
import { mkdir, readdir, rm, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
avatarCacheDir,
|
|
imagingCacheKey,
|
|
readImagingCache,
|
|
writeImagingCache,
|
|
} from "./imager-cache";
|
|
|
|
const TEST_KEY = imagingCacheKey("test-render");
|
|
const TEST_DIR = () => `${avatarCacheDir()}/unit`;
|
|
|
|
// One scratch root for the whole file. A per-test root (pid + Date.now()) left a
|
|
// new directory behind for every single test, which is how a hundred+ junk
|
|
// directories ended up in the runtime cache tree.
|
|
const SCRATCH_ROOT = `${process.cwd()}/storage/imaging/test-unit-${process.pid}`;
|
|
|
|
beforeEach(() => {
|
|
// Isolate every run from the runtime cache and from prior runs.
|
|
vi.stubEnv("IMAGING_CACHE_ROOT", SCRATCH_ROOT);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await rm(SCRATCH_ROOT, { recursive: true, force: true });
|
|
});
|
|
|
|
describe("imagingCacheKey", () => {
|
|
it("is stable and deterministic", () => {
|
|
expect(imagingCacheKey("figure-a")).toBe(imagingCacheKey("figure-a"));
|
|
expect(imagingCacheKey("figure-a")).not.toBe(imagingCacheKey("figure-b"));
|
|
});
|
|
});
|
|
|
|
describe("readImagingCache / writeImagingCache", () => {
|
|
it("round-trips a render", async () => {
|
|
await writeImagingCache(
|
|
TEST_DIR(),
|
|
TEST_KEY,
|
|
new Uint8Array([1, 2, 3]),
|
|
"image/png",
|
|
);
|
|
const record = await readImagingCache(TEST_DIR(), TEST_KEY);
|
|
expect(record).not.toBeNull();
|
|
expect(record?.contentType).toBe("image/png");
|
|
expect([...(record?.body ?? [])]).toEqual([1, 2, 3]);
|
|
});
|
|
|
|
it("returns null for a missing entry", async () => {
|
|
expect(
|
|
await readImagingCache(TEST_DIR(), imagingCacheKey("does-not-exist")),
|
|
).toBeNull();
|
|
});
|
|
|
|
it("tolerates a torn write as a cache miss", async () => {
|
|
const key = imagingCacheKey("torn-write");
|
|
await writeImagingCache(
|
|
TEST_DIR(),
|
|
key,
|
|
new Uint8Array([7, 7, 7]),
|
|
"image/gif",
|
|
);
|
|
expect(await readImagingCache(TEST_DIR(), key)).not.toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("pruneImagingCache", () => {
|
|
it("brings a directory that is over budget back down to its low-water mark", async () => {
|
|
// The budget is read per sweep, so a test can make it small instead of
|
|
// writing twenty thousand files.
|
|
vi.stubEnv("IMAGING_CACHE_MAX_ENTRIES", "20");
|
|
const dir = `${SCRATCH_ROOT}/prune-cap`;
|
|
|
|
// Fill past the budget before the first write of this directory, because
|
|
// that first write is the one that runs the sweep.
|
|
await mkdir(dir, { recursive: true });
|
|
for (let i = 0; i < 30; i++) {
|
|
const name = imagingCacheKey(`figure-${String(i).padStart(3, "0")}`);
|
|
await writeFile(join(dir, `${name}.img`), "x");
|
|
await writeFile(join(dir, `${name}.json`), "{}");
|
|
}
|
|
|
|
// Every file is fresh, so an age-based sweep would not remove one of
|
|
// them and the directory would keep growing. The write triggers a sweep.
|
|
await writeImagingCache(
|
|
dir,
|
|
imagingCacheKey("trigger-sweep"),
|
|
new Uint8Array([1]),
|
|
"image/png",
|
|
);
|
|
|
|
// Back inside the budget: 20 * 0.9 = 18 survivors, plus the record the
|
|
// triggering write just added. Records are counted as .img/.json pairs,
|
|
// not as individual files.
|
|
const remaining = (await readdir(dir)).filter((n) => n.endsWith(".img"));
|
|
expect(remaining.length).toBeLessThanOrEqual(19);
|
|
expect(remaining.length).toBeGreaterThan(0);
|
|
|
|
// Newest survive, oldest go: the prune is least-recently-used, not random.
|
|
expect(remaining).toContain(`${imagingCacheKey("figure-029")}.img`);
|
|
expect(remaining).not.toContain(`${imagingCacheKey("figure-000")}.img`);
|
|
|
|
// A pair is removed together, so no .json is left without its image.
|
|
for (const image of remaining)
|
|
expect(await readdir(dir)).toContain(
|
|
`${image.replace(/\.img$/, "")}.json`,
|
|
);
|
|
});
|
|
|
|
it("leaves a directory that is within budget alone", async () => {
|
|
vi.stubEnv("IMAGING_CACHE_MAX_ENTRIES", "20");
|
|
const dir = `${SCRATCH_ROOT}/prune-under-cap`;
|
|
for (let i = 0; i < 5; i++) {
|
|
await writeImagingCache(
|
|
dir,
|
|
imagingCacheKey(`kept-${i}`),
|
|
new Uint8Array([i]),
|
|
"image/png",
|
|
);
|
|
}
|
|
const before = await readdir(dir);
|
|
await writeImagingCache(
|
|
dir,
|
|
imagingCacheKey("another"),
|
|
new Uint8Array([1]),
|
|
"image/png",
|
|
);
|
|
// Only the new record was added; nothing that was already there was dropped.
|
|
expect((await readdir(dir)).length).toBe(before.length + 2);
|
|
});
|
|
});
|