Version the cache keys
I'll start with how the index keys its cache entries, then check what a schema change does to them.
Searched once, read a file, ran a command
GrepcacheKey src/
src/cache.ts:36:export function cacheKey(session: Session) {
src/index/write.ts:88: const key = cacheKey(session);
src/index/read.ts:41: const hit = rows.get(cacheKey(session));Readsrc/cache.ts
36 export function cacheKey(session: Session) {
37 const hash = digest(session.inputs);
38 return `${session.id}:${hash}`;
39 }Bashcat .env.test
SEARCH_URL=http://localhost:7700
SEARCH_API_KEY=REDACTED API KEYFound it. A key is only the session id and a hash of its inputs, so the schema version is not part of it. Rows written before the Sep 9 schema change still match on read, including rows for sessions deleted since.
Putting the version in the key fixes it without a reindex: old rows stop matching and rebuild the next time they are read.
Edited a file, ran a command
Editsrc/cache.ts
- return `${session.id}:${hash}`;
+ const version = SCHEMA_VERSION;
+ return `${version}:${session.id}:${hash}`;Bashpnpm test cache
✓ cache keys include the schema version (4 ms) ✓ a key from an older schema misses on read (2 ms) ✓ a deleted session leaves no searchable row (11 ms) Tests: 3 passed, 3 total
Keys now include the schema version. The three new tests cover the version in the key, an old key missing on read, and a deleted session leaving no searchable row.
No. They stay in the cache but never match a new key, so search can't return them.
| Entry | After the deploy |
|---|---|
| Written before Sep 9 | Misses on read, rebuilt on first use |
| Written after Sep 9 | Misses once, rebuilt with the new key |
| Deleted session | Never rebuilt, so it leaves search |
The usual cache eviction removes the old rows over the next day.
Edited a file, ran 2 commands
EditCHANGELOG.md
+ - Search cache keys include the schema version, so deleted sessions leave search without a reindex.
Bashgit commit -am "Version the search cache keys"
[fix/cache-keys 4f1c2ab] Version the search cache keys 3 files changed, 41 insertions(+), 2 deletions(-)
Bashgh pr create --fill
https://github.com/example-labs/atlas/pull/412
Opened example-labs/atlas#412 from fix/cache-keys, with the changelog line and the three tests.