cultcache-ts: an injectable cache clock
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3760c02eab
commit
56b2979d0c
5 changed files with 97 additions and 2 deletions
|
|
@ -5,6 +5,10 @@ All notable changes to `@gamecult/cultcache-ts` are recorded here, following
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- `new CultCache({ clock })` and a settable `cache.clock` (`() => Date`; `new Date()` by default): the one clock that stamps every fresh write, as the C# `Clock` and the Rust `set_clock` do. `CultCacheOptions` is exported. A store's restamp of a replacing write already read no clock (`laterStoredAt`), so a pinned clock still stamps a replacing write one millisecond later. A pinned clock alone is not byte-identity where a writer mints random values of its own (the C# writer's unset element ids); this writer mints none.
|
||||
|
||||
## [0.16.0] - 2026-11-14
|
||||
|
||||
### Added
|
||||
|
|
|
|||
|
|
@ -152,6 +152,7 @@ const settings = cache.getRequiredGlobal(settingsDocument);
|
|||
- `defineDocumentType(...)`
|
||||
- `defineDocumentRegistry(...)`
|
||||
- `CultCache.builder()`
|
||||
- `new CultCache({ clock })` and the settable `cache.clock` (`() => Date`, `new Date()` by default): the one clock that stamps fresh writes. Pin it to write reproducible stores; a replacing write is still stamped later than the record it replaces. A pinned clock alone is not byte-identity where a writer mints random values of its own (the C# writer's unset element ids); this writer mints none.
|
||||
- `registerDocumentType(...)`
|
||||
- `registerRegistry(...)`
|
||||
- `registerNameLookup(...)`
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
// (node:fs, koffi). Bring your own CacheBackingStore, or none.
|
||||
export * as codecs from "./codec";
|
||||
export type { Codec, SlotsCodec } from "./codec";
|
||||
export { CultCache, CultCacheBuilder, type CultCacheForeignRecord } from "./cult-cache";
|
||||
export { CultCache, CultCacheBuilder, type CultCacheForeignRecord, type CultCacheOptions } from "./cult-cache";
|
||||
export { inspectCultCacheBytes } from "./cult-cache-inspector";
|
||||
export {
|
||||
decodeDocumentPayload,
|
||||
|
|
|
|||
|
|
@ -194,9 +194,26 @@ function registryDefinitions(
|
|||
: (registry as CultCacheDocumentRegistry).definitions;
|
||||
}
|
||||
|
||||
export interface CultCacheOptions {
|
||||
/** The one source of "now" for every stamp this cache mints; the wall clock by default. Pin it to write reproducible stores. */
|
||||
clock?: () => Date;
|
||||
}
|
||||
|
||||
export class CultCache {
|
||||
static readonly GLOBAL_KEY = "__global__";
|
||||
|
||||
/**
|
||||
* The one source of "now" for every stamp this cache mints (`new Date()` by default). Pin it to write reproducible stores. A
|
||||
* replacing write is still stamped later than the record it replaces, derived from the two stamps and no clock. A pinned clock
|
||||
* alone is not byte-identity where a writer mints random values of its own (the C# writer's unset element ids); this writer
|
||||
* mints none.
|
||||
*/
|
||||
clock: () => Date;
|
||||
|
||||
constructor(options: CultCacheOptions = {}) {
|
||||
this.clock = options.clock ?? (() => new Date());
|
||||
}
|
||||
|
||||
static {
|
||||
registerNow = (cache, definition) => cache.#registerNow(definition);
|
||||
attachNow = (cache, store, types) => cache.#attachNow(store, types);
|
||||
|
|
@ -830,7 +847,7 @@ export class CultCache {
|
|||
key,
|
||||
type: definition.type,
|
||||
payload: this.#cloneBytes(payload),
|
||||
storedAt: new Date().toISOString(),
|
||||
storedAt: this.clock().toISOString(),
|
||||
schemaId: registered.catalogEntry.schemaId,
|
||||
catalogEntry: registered.catalogEntry,
|
||||
};
|
||||
|
|
|
|||
73
packages/cultcache-ts/test/cache-clock.test.ts
Normal file
73
packages/cultcache-ts/test/cache-clock.test.ts
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import assert from "node:assert/strict";
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
import { z } from "zod";
|
||||
|
||||
import { CultCache, SingleFileMessagePackBackingStore, defineDocumentType } from "../src/index";
|
||||
|
||||
const settings = defineDocumentType({
|
||||
type: "settings",
|
||||
schemaId: "gamecult.clock_settings.v1",
|
||||
schemaName: "gamecult.clock_settings",
|
||||
schemaVersion: "gamecult.clock_settings.v1",
|
||||
schema: z.object({ theme: z.string(), retries: z.number().int() }),
|
||||
});
|
||||
const pinned = () => new Date("2026-03-04T05:06:07.000Z");
|
||||
|
||||
async function withDir(run: (dir: string) => Promise<void>): Promise<void> {
|
||||
const dir = await mkdtemp(join(tmpdir(), "cultcache-clock-"));
|
||||
try {
|
||||
await run(dir);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function pinnedCache(path: string | null, clock = pinned): Promise<CultCache> {
|
||||
const cache = new CultCache({ clock });
|
||||
await cache.registerDocumentType(settings);
|
||||
if (path !== null) {
|
||||
await cache.addGenericBackingStore(new SingleFileMessagePackBackingStore(path));
|
||||
}
|
||||
await cache.pullAllBackingStores();
|
||||
return cache;
|
||||
}
|
||||
|
||||
test("a cache with no clock stamps near now", async () => {
|
||||
const cache = new CultCache();
|
||||
await cache.registerDocumentType(settings);
|
||||
await cache.put(settings, "app", { theme: "green", retries: 1 });
|
||||
const stamped = Date.parse(cache.getEnvelope(settings, "app")!.storedAt);
|
||||
assert.ok(Math.abs(Date.now() - stamped) < 5000);
|
||||
});
|
||||
|
||||
test("two replays with a pinned clock write identical bytes", async () => {
|
||||
await withDir(async (dir) => {
|
||||
for (const name of ["a.cc", "b.cc"]) {
|
||||
const cache = await pinnedCache(join(dir, name));
|
||||
await cache.put(settings, "app", { theme: "green", retries: 1 });
|
||||
await cache.put(settings, "app", { theme: "blue", retries: 2 });
|
||||
await cache.put(settings, "other", { theme: "green", retries: 1 });
|
||||
}
|
||||
assert.deepEqual(await readFile(join(dir, "a.cc")), await readFile(join(dir, "b.cc")));
|
||||
});
|
||||
});
|
||||
|
||||
test("a pinned clock still stamps a replacing write later", async () => {
|
||||
await withDir(async (dir) => {
|
||||
const cache = await pinnedCache(join(dir, "later.cc"));
|
||||
await cache.put(settings, "app", { theme: "green", retries: 1 });
|
||||
assert.equal(cache.getEnvelope(settings, "app")!.storedAt, "2026-03-04T05:06:07.000Z");
|
||||
await cache.put(settings, "app", { theme: "blue", retries: 2 });
|
||||
assert.equal(cache.getEnvelope(settings, "app")!.storedAt, "2026-03-04T05:06:07.001Z");
|
||||
});
|
||||
});
|
||||
|
||||
test("the clock property is settable and the next write reads it", async () => {
|
||||
const cache = await pinnedCache(null);
|
||||
cache.clock = () => new Date("2030-01-01T00:00:00.000Z");
|
||||
await cache.put(settings, "app", { theme: "green", retries: 1 });
|
||||
assert.equal(cache.getEnvelope(settings, "app")!.storedAt, "2030-01-01T00:00:00.000Z");
|
||||
});
|
||||
Loading…
Reference in a new issue