-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcache-engine.ts
More file actions
95 lines (88 loc) · 2.43 KB
/
cache-engine.ts
File metadata and controls
95 lines (88 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* FILE: cache-engine.ts
* LAYER: infra/storage
* ---------------------------------------------------------------------
* PURPOSE:
* Lightweight offline-first snapshot cache. NOT persistent.
* Used by services and query layers as a minimal data cache before
* integrating MMKV/SQLite-based persistence.
*
* Designed to support:
* - stale-while-revalidate patterns
* - offline fallback
* - domain-level prefetching
* - high-level services using transport.query()
*
* RESPONSIBILITIES:
* - setSnapshot(key, data)
* - getSnapshot(key)
* - removeSnapshot(key)
* - clear()
* - (future) TTL, versioning, hydration, serialization
*
* DATA-FLOW:
* ONLINE:
* transport.query()
* → cacheEngine.setSnapshot()
* → UI reads via cacheEngine.getSnapshot()
*
* OFFLINE:
* transport.query() throws offline
* → UI falls back to cacheEngine.getSnapshot()
*
* EXTENSION GUIDELINES:
* - Replace Map with MMKV/SQLite for persistence.
* - Add TTL per entry:
* MEMORY_CACHE.set(key, { data, cachedAt, ttlMs })
*
* - Add versioning per domain:
* { version: schemaVersion, data }
*
* - Add "listeners" (pub/sub):
* cacheEngine.subscribe(key, callback)
*
* - Keep API stable — do NOT change public signatures.
*
* THREAD SAFETY:
* RN JS runtime is single-threaded → Map is safe.
* When migrating to native storage, ensure atomic writes.
* ---------------------------------------------------------------------
*/
type CacheValue = unknown
const MEMORY_CACHE = new Map<string, CacheValue>()
export const cacheEngine = {
/**
* Save snapshot under the provided key.
* Keys SHOULD be namespaced (e.g., "user.profile", "feed.home", etc.)
*/
setSnapshot(key: string, value: CacheValue) {
MEMORY_CACHE.set(key, value)
},
/**
* Retrieve previously cached snapshot.
* Returns undefined if nothing is cached.
*/
getSnapshot<T>(key: string): T | undefined {
return MEMORY_CACHE.get(key) as T | undefined
},
/**
* Remove specific snapshot entry.
*/
removeSnapshot(key: string) {
MEMORY_CACHE.delete(key)
},
/**
* Clear entire snapshot cache.
* Called typically on logout or environment reset.
*/
clear() {
MEMORY_CACHE.clear()
},
/**
* FUTURE API:
* - getMeta(key) → TTL, version
* - subscribe(key, cb) → pub/sub
* - hydrateFromMMKV()
* - persistToMMKV()
*/
}