Persistence
Only the SharedWorker hosting persists. Its server holds a HostedPersistence, which loads the persister and the IndexedDB adapter as a chunk of their own, with import(), the first time a cache with persist: true is opened or clearAll runs. The dedicated-worker and in-process hostings persist nothing; where IndexedDB exists, their clearAll only empties the database. This page follows a persisted cache through its restore, its write-back and a quota stop. The persistence guide covers the options and the guarantees.
Restore
cache()waits for the load. The server creates the cache, then, for a persisted cache, answers only once the persistence'sopenhas settled. No call through a handle can therefore reach a cache whose load is pending.- The chunk loads on demand. Until the chunk has loaded, the server's calls on that cache wait in arrival order. A chunk that fails to load, or has not loaded within the deadline (
timeoutMs, default 5000 ms), turns persistence off for the worker's life: every waiting and later open stops with'unavailable'. - The load has one deadline. The persister's load must finish within the same
timeoutMs, counted from theopencall, the chunk's import included; a load still out then stops the cache with'unavailable', andcache()answers. A store that cannot be opened stops every tracked cache with'unavailable'; a read that fails stops the cache with'error'. - Restore applies into an empty cache.
CacheState.restorechecks the cache record against the cache's structure and version. A record that matches adopts its version, moves the watermark forwards only, and writes each valid page's coverage, minus the provisional region, with the page's points. A record of another structure or version, or pages without a record, are not applied, and the cache is rewritten from memory at the next flush. - A change wins over the load. If anything mutates the cache while its load is still out, the load is abandoned: nothing stored is applied, and the next flush rewrites the cache from memory.
Write-back
The diagram shows a cache whose persistence is active. A superseded worker writes only the deletions of its removals (step 3).
- The engine reports, the persister decides. Every mutating call on a cache reports what it changed through the cache's hooks: a write over some slots, clipped below the provisional region, a watermark change, a removal, a clear, or a version change. The engine sets no timers; the persister does.
- Additions wait for a quiet spell. After a write, the cache's flush is due
debounceMs(default 1000 ms) after the last change, but no later thanmaxDelayMs(default 5000 ms) after the first. - Removals are due at once. A
removeorclearchange makes the flush due immediately. The server runs every op that names a cache through the persistence: when the op recorded a removal, its answer, success or failure, comes once that removal is durable or, after the cache's persistence has stopped, once the persister releases it (architecture §4.10.1; What can outlive a removal says what the answer guarantees after a stop). A superseded worker has stopped every cache already, and still tries to write each removal as a deletion that the call waits for: see Failures. - One writer. The persister runs one job at a time, removal-carrying jobs first. A flush builds its plan from memory alone, without reading the disk: the dirty pages' records (or every record after a clear or a version change) and the cache record. The store writes the plan in one transaction, with strict durability when it carries a removal.
- The last tab. When the server's last connection goes, it asks the persistence to flush, which makes every dirty cache's flush due now.
Quota stop
- A full disk stops the cache. A write rejected with a
QuotaExceededErrorstops that cache's persistence at once, without a retry. Its dirty state and queued jobs are dropped. - Every tab hears it. The persister calls its
onStopped, which the SharedWorker's server turns into apersistStoppedevent for every ready port, with the cache id, the reason and the error. On the page,client.on('persistStopped')and the handle'son(filtered to its cache) receive it. - A cleanup follows. A cleanup job tries to delete the cache's records; its failure is ignored, and records can then remain on disk.
- Other failures retry first. Any other rejected write puts its dirty state back and is retried after
debounceMs, under the deadline it had (timeoutMsfrom when the flush was due). When the deadline passes, the cache stops with'error', or'unavailable'when no attempt had failed yet.
Drawn from:
packages/tscache/src/rpc/server.ts#RpcServerpackages/tscache/src/rpc/server.ts#Persistencepackages/tscache/src/entries/worker.ts#createServerpackages/tscache/src/persist/hosted.ts#HostedPersistencepackages/tscache/src/persist/clear-only.ts#ClearOnlyPersistencepackages/tscache/src/persist/clear-only.ts#lazyIdbStorepackages/tscache/src/persist/persister.ts#Persisterpackages/tscache/src/persist/persister.ts#PersisterOptionspackages/tscache/src/persist/persister.ts#durablypackages/tscache/src/persist/store.ts#PersistStorepackages/tscache/src/persist/store.ts#WritePlanpackages/tscache/src/persist/idb-store.ts#openIdbStorepackages/tscache/src/engine/engine.ts#observeCachespackages/tscache/src/engine/cache.ts#CacheStatepackages/tscache/src/engine/cache.ts#CacheChangepackages/tscache/src/engine/restore.ts#restorepackages/tscache/src/client/cache.ts#CacheHandle