Skip to main content

LocalCache

LocalCache keeps bytes and text on the player's device, for things the deck can rebuild: downloaded or unpacked assets, compiled data. Entries belong to the deck whose code is running. In the app, decks loaded from the same web address share entries.

Any entry can be gone at any time. Each deck has 20 MB; past that, its own least recently used entries are evicted. On the web all decks together have 300 MB; past that, the least recently used entries of any deck are evicted. Both are less when the device is short of space. The browser or app may also clear the whole cache. Never keep anything here that the deck cannot make again; use Store for that.

  • LocalCache.getBinary(key): Promise<ArrayBuffer | null> — the bytes at key, or null if there are none. A text entry comes back as UTF-8.
  • LocalCache.getText(key): Promise<string | null> — the text at key. A bytes entry is decoded as UTF-8.
  • LocalCache.set(key, value): Promise<void> — stores an ArrayBuffer, a Uint8Array or a string. An ArrayBuffer is moved, not copied, and is empty (detached) once set returns; pass buffer.slice(0) to keep one. A Uint8Array that covers its whole buffer moves that buffer the same way; one over part of a buffer is copied.
  • LocalCache.remove(key): Promise<void> — removes key.

Keys are 1 to 1024 characters, without NUL; a bad key or value throws. Nothing else does: a get that cannot read returns null, and a set that cannot write stores nothing.

let bytes = await LocalCache.getBinary("assets-v3");
if (!bytes) {
bytes = await unpackAssets();
LocalCache.set("assets-v3", bytes.slice(0));
}