Skip to content

Gotchas

An empty prefix matches nothing. Use cache.clear() to wipe everything.

Correct
// Use clear() to wipe the entire cache
cache.clear()

invalidate() is prefix-based, not exact-match

Section titled “invalidate() is prefix-based, not exact-match”

A prefix matches every key that starts with it, including nested sub-keys.

// invalidate(['order', 'details', '42']) also matches:
// ['order', 'details', '42']
// ['order', 'details', '42', 'comments']
// ['order', 'details', '42', 'attachments']
//
// It does NOT match:
// ['order', 'details', '43']
// ['order', 'list']

ref.set() and ref.update() write to the cache

Section titled “ref.set() and ref.update() write to the cache”

They update both the Angular resource and the DataCache. Optimistic values survive cache version bumps from unrelated invalidations. Call invalidate() to trigger a fresh server fetch.

// set() and update() write to both the Angular resource AND the DataCache.
// Optimistic values survive cache version bumps from unrelated invalidations.
ref.set(newValue)
ref.update(prev => ({ ...prev, name: 'updated' }))
// To trigger a fresh server fetch after an optimistic update:
cache.invalidate(['order', 'details', '42'])

DataCache stores unknown internally. Type correctness depends on consistent key-to-type pairings in your code.

// Nothing prevents this: both compile fine
cache.set(['user', '1'], { name: 'Alice' }) // User
const entry = cache.get<Order[]>(['user', '1']) // reads as Order[]
// Convention: one key prefix per type, enforced in your API service