@fjell/cache 4.7.35 → 4.7.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -7
- package/CACHE_EVENTS.md +0 -306
- package/CACHE_IMPLEMENTATIONS.md +0 -315
- package/CONFIGURATION_GUIDE.md +0 -209
- package/CRITICAL_FIXES.md +0 -68
- package/MEMORY_LEAK_FIXES.md +0 -270
- package/MIGRATION_v3.md +0 -249
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fjell/cache",
|
|
3
3
|
"description": "Cache for Fjell",
|
|
4
|
-
"version": "4.7.
|
|
4
|
+
"version": "4.7.36",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cache",
|
|
7
7
|
"fjell"
|
|
@@ -37,11 +37,11 @@
|
|
|
37
37
|
"docs:test": "cd docs && npm run test"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@fjell/client-api": "^4.4.
|
|
41
|
-
"@fjell/core": "^4.4.
|
|
42
|
-
"@fjell/http-api": "^4.4.
|
|
43
|
-
"@fjell/logging": "^4.4.
|
|
44
|
-
"@fjell/registry": "^4.4.
|
|
40
|
+
"@fjell/client-api": "^4.4.47",
|
|
41
|
+
"@fjell/core": "^4.4.51",
|
|
42
|
+
"@fjell/http-api": "^4.4.43",
|
|
43
|
+
"@fjell/logging": "^4.4.50",
|
|
44
|
+
"@fjell/registry": "^4.4.53",
|
|
45
45
|
"fast-safe-stringify": "^2.1.1",
|
|
46
46
|
"flatted": "^3.3.3",
|
|
47
47
|
"yocto-queue": "^1.2.1"
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@eslint/eslintrc": "^3.3.1",
|
|
51
51
|
"@eslint/js": "^9.33.0",
|
|
52
|
-
"@fjell/eslint-config": "^1.1.
|
|
52
|
+
"@fjell/eslint-config": "^1.1.28",
|
|
53
53
|
"@swc/core": "^1.13.3",
|
|
54
54
|
"@tsconfig/recommended": "^1.0.10",
|
|
55
55
|
"@types/multer": "^2.0.0",
|
package/CACHE_EVENTS.md
DELETED
|
@@ -1,306 +0,0 @@
|
|
|
1
|
-
# Cache Event System
|
|
2
|
-
|
|
3
|
-
The fjell-cache event system provides real-time notifications when cache operations occur. This enables reactive patterns where components can subscribe to cache changes and update automatically.
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
The event system is built into the core Cache interface and provides:
|
|
8
|
-
|
|
9
|
-
- **Event emission** for all cache operations (create, update, remove, query, etc.)
|
|
10
|
-
- **Subscription management** with filtering options
|
|
11
|
-
- **Event types** covering all cache state changes
|
|
12
|
-
- **Debouncing** support for high-frequency updates
|
|
13
|
-
- **Type safety** with full TypeScript support
|
|
14
|
-
|
|
15
|
-
## Basic Usage
|
|
16
|
-
|
|
17
|
-
### Subscribing to Events
|
|
18
|
-
|
|
19
|
-
```typescript
|
|
20
|
-
import { createCache, CacheEventListener } from '@fjell/cache';
|
|
21
|
-
|
|
22
|
-
// Create cache
|
|
23
|
-
const cache = createCache(api, coordinate, registry);
|
|
24
|
-
|
|
25
|
-
// Subscribe to all events
|
|
26
|
-
const listener: CacheEventListener<MyItem, 'myType'> = (event) => {
|
|
27
|
-
console.log('Cache event:', event.type, event);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const subscription = cache.subscribe(listener);
|
|
31
|
-
|
|
32
|
-
// Later: unsubscribe
|
|
33
|
-
subscription.unsubscribe();
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### Filtering Events
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
// Subscribe only to item creation events
|
|
40
|
-
const subscription = cache.subscribe(listener, {
|
|
41
|
-
eventTypes: ['item_created', 'item_updated']
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
// Subscribe only to events for specific keys
|
|
45
|
-
const subscription = cache.subscribe(listener, {
|
|
46
|
-
keys: [{ pk: 'user-123' }]
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Subscribe only to events in specific locations
|
|
50
|
-
const subscription = cache.subscribe(listener, {
|
|
51
|
-
locations: [{ lk: 'container-1' }]
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
// Subscribe with debouncing (useful for UI updates)
|
|
55
|
-
const subscription = cache.subscribe(listener, {
|
|
56
|
-
debounceMs: 100 // Debounce events by 100ms
|
|
57
|
-
});
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## Event Types
|
|
61
|
-
|
|
62
|
-
### Item Events
|
|
63
|
-
|
|
64
|
-
These events are emitted when individual items are affected:
|
|
65
|
-
|
|
66
|
-
- **`item_created`** - Item was created via API and cached
|
|
67
|
-
- **`item_updated`** - Item was updated via API and cache updated
|
|
68
|
-
- **`item_removed`** - Item was removed via API and from cache
|
|
69
|
-
- **`item_retrieved`** - Item was retrieved from API and cached
|
|
70
|
-
- **`item_set`** - Item was set directly in cache (no API call)
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
interface ItemEvent<V, S, L1, L2, L3, L4, L5> {
|
|
74
|
-
type: 'item_created' | 'item_updated' | 'item_removed' | 'item_retrieved' | 'item_set';
|
|
75
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>;
|
|
76
|
-
item: V | null; // null for removed items
|
|
77
|
-
previousItem?: V | null; // Previous state before change
|
|
78
|
-
affectedLocations?: LocKeyArray<L1, L2, L3, L4, L5> | [];
|
|
79
|
-
timestamp: number;
|
|
80
|
-
source: 'api' | 'cache' | 'operation';
|
|
81
|
-
}
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### Query Events
|
|
85
|
-
|
|
86
|
-
These events are emitted when multiple items are queried:
|
|
87
|
-
|
|
88
|
-
- **`items_queried`** - Multiple items were queried and cached
|
|
89
|
-
|
|
90
|
-
```typescript
|
|
91
|
-
interface QueryEvent<V, S, L1, L2, L3, L4, L5> {
|
|
92
|
-
type: 'items_queried';
|
|
93
|
-
query: ItemQuery;
|
|
94
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [];
|
|
95
|
-
items: V[];
|
|
96
|
-
affectedKeys: (ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>)[];
|
|
97
|
-
timestamp: number;
|
|
98
|
-
source: 'api' | 'cache' | 'operation';
|
|
99
|
-
}
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
### Cache Management Events
|
|
103
|
-
|
|
104
|
-
These events are emitted for cache-wide operations:
|
|
105
|
-
|
|
106
|
-
- **`cache_cleared`** - Entire cache was cleared
|
|
107
|
-
- **`location_invalidated`** - Specific location(s) were invalidated
|
|
108
|
-
- **`query_invalidated`** - Cached query results were invalidated
|
|
109
|
-
|
|
110
|
-
## Advanced Usage
|
|
111
|
-
|
|
112
|
-
### React Integration Example
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
import { useEffect, useState } from 'react';
|
|
116
|
-
import { Cache, CacheSubscription } from '@fjell/cache';
|
|
117
|
-
|
|
118
|
-
function useItem<T>(cache: Cache<T, any>, key: any): T | null {
|
|
119
|
-
const [item, setItem] = useState<T | null>(() =>
|
|
120
|
-
cache.cacheMap.get(key)
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
useEffect(() => {
|
|
124
|
-
const subscription = cache.subscribe((event) => {
|
|
125
|
-
if (event.type === 'item_changed' &&
|
|
126
|
-
JSON.stringify(event.key) === JSON.stringify(key)) {
|
|
127
|
-
setItem(event.item);
|
|
128
|
-
} else if (event.type === 'item_removed' &&
|
|
129
|
-
JSON.stringify(event.key) === JSON.stringify(key)) {
|
|
130
|
-
setItem(null);
|
|
131
|
-
}
|
|
132
|
-
}, {
|
|
133
|
-
keys: [key],
|
|
134
|
-
eventTypes: ['item_updated', 'item_removed', 'item_set']
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
return () => subscription.unsubscribe();
|
|
138
|
-
}, [cache, key]);
|
|
139
|
-
|
|
140
|
-
return item;
|
|
141
|
-
}
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
### Listening to Multiple Caches
|
|
145
|
-
|
|
146
|
-
```typescript
|
|
147
|
-
class CacheEventHub {
|
|
148
|
-
private subscriptions: CacheSubscription[] = [];
|
|
149
|
-
|
|
150
|
-
subscribeTo<T>(cache: Cache<T, any>, listener: CacheEventListener<T, any>) {
|
|
151
|
-
const subscription = cache.subscribe(listener);
|
|
152
|
-
this.subscriptions.push(subscription);
|
|
153
|
-
return subscription;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
unsubscribeAll() {
|
|
157
|
-
this.subscriptions.forEach(sub => sub.unsubscribe());
|
|
158
|
-
this.subscriptions = [];
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const hub = new CacheEventHub();
|
|
163
|
-
|
|
164
|
-
// Subscribe to multiple caches
|
|
165
|
-
hub.subscribeTo(userCache, (event) => console.log('User event:', event));
|
|
166
|
-
hub.subscribeTo(orderCache, (event) => console.log('Order event:', event));
|
|
167
|
-
|
|
168
|
-
// Later: clean up all subscriptions
|
|
169
|
-
hub.unsubscribeAll();
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
### Event Logging and Debugging
|
|
173
|
-
|
|
174
|
-
```typescript
|
|
175
|
-
// Log all cache events for debugging
|
|
176
|
-
const debugSubscription = cache.subscribe((event) => {
|
|
177
|
-
console.log(`[${new Date(event.timestamp).toISOString()}] ${event.type}:`, {
|
|
178
|
-
source: event.source,
|
|
179
|
-
...('key' in event ? { key: event.key } : {}),
|
|
180
|
-
...('query' in event ? { query: event.query } : {}),
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
// Performance monitoring
|
|
185
|
-
let eventCounts = new Map<string, number>();
|
|
186
|
-
|
|
187
|
-
cache.subscribe((event) => {
|
|
188
|
-
const count = eventCounts.get(event.type) || 0;
|
|
189
|
-
eventCounts.set(event.type, count + 1);
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
setInterval(() => {
|
|
193
|
-
console.log('Event counts:', Object.fromEntries(eventCounts));
|
|
194
|
-
eventCounts.clear();
|
|
195
|
-
}, 10000);
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
## Best Practices
|
|
199
|
-
|
|
200
|
-
### 1. Use Specific Event Filters
|
|
201
|
-
|
|
202
|
-
```typescript
|
|
203
|
-
// Good: Filter by specific event types and keys
|
|
204
|
-
cache.subscribe(listener, {
|
|
205
|
-
eventTypes: ['item_updated'],
|
|
206
|
-
keys: [userKey]
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
// Avoid: Subscribing to all events when you only need specific ones
|
|
210
|
-
cache.subscribe(listener); // Too broad
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
### 2. Debounce High-Frequency Updates
|
|
214
|
-
|
|
215
|
-
```typescript
|
|
216
|
-
// Good: Debounce UI updates
|
|
217
|
-
cache.subscribe(updateUI, {
|
|
218
|
-
debounceMs: 100
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
// Good: Don't debounce critical business logic
|
|
222
|
-
cache.subscribe(saveToDisk, {
|
|
223
|
-
// No debouncing for critical operations
|
|
224
|
-
});
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
### 3. Clean Up Subscriptions
|
|
228
|
-
|
|
229
|
-
```typescript
|
|
230
|
-
useEffect(() => {
|
|
231
|
-
const subscription = cache.subscribe(listener);
|
|
232
|
-
|
|
233
|
-
// Always clean up
|
|
234
|
-
return () => subscription.unsubscribe();
|
|
235
|
-
}, []);
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
### 4. Handle Errors in Listeners
|
|
239
|
-
|
|
240
|
-
```typescript
|
|
241
|
-
cache.subscribe((event) => {
|
|
242
|
-
try {
|
|
243
|
-
// Your event handling logic
|
|
244
|
-
handleEvent(event);
|
|
245
|
-
} catch (error) {
|
|
246
|
-
console.error('Error handling cache event:', error);
|
|
247
|
-
// Don't let errors break other listeners
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
## Event Sources
|
|
253
|
-
|
|
254
|
-
Events include a `source` field indicating where they originated:
|
|
255
|
-
|
|
256
|
-
- **`api`** - Event from API operation (create, update, remove, etc.)
|
|
257
|
-
- **`cache`** - Event from direct cache operation (set, invalidate)
|
|
258
|
-
- **`operation`** - Event from internal cache operation
|
|
259
|
-
|
|
260
|
-
## Performance Considerations
|
|
261
|
-
|
|
262
|
-
- **Subscription count**: Each subscription has minimal overhead, but avoid creating unnecessary subscriptions
|
|
263
|
-
- **Event frequency**: Use debouncing for high-frequency UI updates
|
|
264
|
-
- **Memory leaks**: Always unsubscribe when components unmount
|
|
265
|
-
- **Event filtering**: Use specific filters to reduce unnecessary event processing
|
|
266
|
-
|
|
267
|
-
## Error Handling
|
|
268
|
-
|
|
269
|
-
The event system is designed to be resilient:
|
|
270
|
-
|
|
271
|
-
- Listener errors don't affect other listeners or cache operations
|
|
272
|
-
- Failed subscriptions are automatically cleaned up
|
|
273
|
-
- Event emission continues even if some listeners fail
|
|
274
|
-
|
|
275
|
-
## Migration from Manual State Management
|
|
276
|
-
|
|
277
|
-
If you're currently using manual state management patterns:
|
|
278
|
-
|
|
279
|
-
```typescript
|
|
280
|
-
// Before: Manual state management
|
|
281
|
-
const [items, setItems] = useState([]);
|
|
282
|
-
|
|
283
|
-
const addItem = async (item) => {
|
|
284
|
-
const newItem = await cache.operations.create(item);
|
|
285
|
-
setItems(prev => [...prev, newItem]); // Manual update
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
// After: Event-driven updates
|
|
289
|
-
const [items, setItems] = useState([]);
|
|
290
|
-
|
|
291
|
-
useEffect(() => {
|
|
292
|
-
const subscription = cache.subscribe((event) => {
|
|
293
|
-
if (event.type === 'item_created') {
|
|
294
|
-
setItems(prev => [...prev, event.item]);
|
|
295
|
-
}
|
|
296
|
-
}, { eventTypes: ['item_created'] });
|
|
297
|
-
|
|
298
|
-
return () => subscription.unsubscribe();
|
|
299
|
-
}, []);
|
|
300
|
-
|
|
301
|
-
const addItem = async (item) => {
|
|
302
|
-
await cache.operations.create(item); // Event automatically updates state
|
|
303
|
-
};
|
|
304
|
-
```
|
|
305
|
-
|
|
306
|
-
This approach eliminates the need for manual state synchronization and ensures your UI always reflects the current cache state.
|
package/CACHE_IMPLEMENTATIONS.md
DELETED
|
@@ -1,315 +0,0 @@
|
|
|
1
|
-
# Cache Implementations Guide
|
|
2
|
-
|
|
3
|
-
The `@fjell/cache` package now provides multiple cache implementations to suit different environments and persistence requirements.
|
|
4
|
-
|
|
5
|
-
## Available Implementations
|
|
6
|
-
|
|
7
|
-
### 1. MemoryCacheMap (Default)
|
|
8
|
-
**Location**: `src/memory/MemoryCacheMap.ts`
|
|
9
|
-
**Use case**: Server-side or Node.js applications
|
|
10
|
-
|
|
11
|
-
```typescript
|
|
12
|
-
import { MemoryCacheMap } from '@fjell/cache';
|
|
13
|
-
|
|
14
|
-
const cache = new MemoryCacheMap<YourItem, 'item-type'>(keyTypeArray);
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
**Characteristics:**
|
|
18
|
-
- Fast, in-memory storage
|
|
19
|
-
- Data lost when application restarts
|
|
20
|
-
- No persistence across sessions
|
|
21
|
-
- Thread-safe for single process
|
|
22
|
-
- Best performance for frequent read/write operations
|
|
23
|
-
|
|
24
|
-
### 2. LocalStorageCacheMap
|
|
25
|
-
**Location**: `src/browser/LocalStorageCacheMap.ts`
|
|
26
|
-
**Use case**: Browser applications requiring persistent storage
|
|
27
|
-
|
|
28
|
-
```typescript
|
|
29
|
-
import { LocalStorageCacheMap } from '@fjell/cache';
|
|
30
|
-
|
|
31
|
-
const cache = new LocalStorageCacheMap<YourItem, 'item-type'>(
|
|
32
|
-
keyTypeArray,
|
|
33
|
-
'my-app-cache' // optional prefix
|
|
34
|
-
);
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
**Characteristics:**
|
|
38
|
-
- ~5-10MB storage limit
|
|
39
|
-
- Data persists across browser sessions and restarts
|
|
40
|
-
- Synchronous operations
|
|
41
|
-
- Shared across all tabs for the same origin
|
|
42
|
-
- Stores data as JSON strings
|
|
43
|
-
|
|
44
|
-
### 3. SessionStorageCacheMap
|
|
45
|
-
**Location**: `src/browser/SessionStorageCacheMap.ts`
|
|
46
|
-
**Use case**: Browser applications requiring tab-scoped temporary storage
|
|
47
|
-
|
|
48
|
-
```typescript
|
|
49
|
-
import { SessionStorageCacheMap } from '@fjell/cache';
|
|
50
|
-
|
|
51
|
-
const cache = new SessionStorageCacheMap<YourItem, 'item-type'>(
|
|
52
|
-
keyTypeArray,
|
|
53
|
-
'my-session-cache' // optional prefix
|
|
54
|
-
);
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
**Characteristics:**
|
|
58
|
-
- ~5MB storage limit
|
|
59
|
-
- Data lost when browser tab is closed
|
|
60
|
-
- Synchronous operations
|
|
61
|
-
- Tab-specific storage (not shared between tabs)
|
|
62
|
-
- Stores data as JSON strings
|
|
63
|
-
|
|
64
|
-
### 4. AsyncIndexDBCacheMap
|
|
65
|
-
**Location**: `src/browser/AsyncIndexDBCacheMap.ts`
|
|
66
|
-
**Use case**: Browser applications requiring large, structured data storage
|
|
67
|
-
|
|
68
|
-
```typescript
|
|
69
|
-
import { AsyncIndexDBCacheMap } from '@fjell/cache';
|
|
70
|
-
|
|
71
|
-
const cache = new AsyncIndexDBCacheMap<YourItem, 'item-type'>(
|
|
72
|
-
keyTypeArray,
|
|
73
|
-
'my-database', // database name
|
|
74
|
-
'cache-store', // object store name
|
|
75
|
-
1 // version
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
// All operations are async
|
|
79
|
-
const item = await cache.get(key);
|
|
80
|
-
await cache.set(key, value);
|
|
81
|
-
const items = await cache.allIn(locations);
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
**Characteristics:**
|
|
85
|
-
- Hundreds of MB+ storage capacity
|
|
86
|
-
- Asynchronous operations (returns Promises)
|
|
87
|
-
- Long-term persistence
|
|
88
|
-
- Can store complex objects natively
|
|
89
|
-
- Better performance for large datasets
|
|
90
|
-
- Supports transactions and indexing
|
|
91
|
-
|
|
92
|
-
### 5. IndexDBCacheMap (Synchronous Wrapper)
|
|
93
|
-
**Location**: `src/browser/IndexDBCacheMap.ts`
|
|
94
|
-
**Use case**: Browser applications needing synchronous API with IndexedDB persistence
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
import { IndexDBCacheMap } from '@fjell/cache';
|
|
98
|
-
|
|
99
|
-
const cache = new IndexDBCacheMap<YourItem, 'item-type'>(
|
|
100
|
-
keyTypeArray,
|
|
101
|
-
'my-database', // database name
|
|
102
|
-
'cache-store', // object store name
|
|
103
|
-
1 // version
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
// Synchronous operations work immediately
|
|
107
|
-
cache.set(key, value); // Sets in memory cache immediately
|
|
108
|
-
const item = cache.get(key); // Gets from memory cache immediately
|
|
109
|
-
const items = cache.allIn(locations);
|
|
110
|
-
|
|
111
|
-
// Background sync to IndexedDB happens automatically
|
|
112
|
-
// For explicit async operations, use:
|
|
113
|
-
await cache.asyncCache.set(key, value);
|
|
114
|
-
const item = await cache.asyncCache.get(key);
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
**Characteristics:**
|
|
118
|
-
- Synchronous API compatible with other CacheMap implementations
|
|
119
|
-
- Memory cache for immediate operations
|
|
120
|
-
- Background IndexedDB sync for persistence
|
|
121
|
-
- Higher memory usage (dual storage)
|
|
122
|
-
- Best for apps migrating from synchronous cache implementations
|
|
123
|
-
- Provides access to async cache via `asyncCache` property
|
|
124
|
-
|
|
125
|
-
**When to use IndexDBCacheMap vs AsyncIndexDBCacheMap:**
|
|
126
|
-
- **IndexDBCacheMap**: When you need synchronous compatibility or are migrating from MemoryCacheMap
|
|
127
|
-
- **AsyncIndexDBCacheMap**: When you can work with async/await and want direct IndexedDB control
|
|
128
|
-
|
|
129
|
-
## Migration Guide
|
|
130
|
-
|
|
131
|
-
### From Old CacheMap
|
|
132
|
-
If you were previously using `CacheMap` directly:
|
|
133
|
-
|
|
134
|
-
```typescript
|
|
135
|
-
// OLD
|
|
136
|
-
import { CacheMap } from '@fjell/cache';
|
|
137
|
-
const cache = new CacheMap(keyTypeArray);
|
|
138
|
-
|
|
139
|
-
// NEW
|
|
140
|
-
import { MemoryCacheMap } from '@fjell/cache';
|
|
141
|
-
const cache = new MemoryCacheMap(keyTypeArray);
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
### Choosing the Right Implementation
|
|
145
|
-
|
|
146
|
-
**For Node.js/Server applications:**
|
|
147
|
-
- Use `MemoryCacheMap`
|
|
148
|
-
|
|
149
|
-
**For Browser applications:**
|
|
150
|
-
- **Small data, session-only**: `SessionStorageCacheMap`
|
|
151
|
-
- **Small data, persistent**: `LocalStorageCacheMap`
|
|
152
|
-
- **Large data, complex queries**: `AsyncIndexDBCacheMap`
|
|
153
|
-
|
|
154
|
-
## Cache Information and Capabilities
|
|
155
|
-
|
|
156
|
-
All cache instances expose metadata about their configuration and capabilities through the `getCacheInfo()` method at the Cache level. This provides client applications with visibility into the cache's behavior and limitations.
|
|
157
|
-
|
|
158
|
-
### CacheInfo Interface
|
|
159
|
-
|
|
160
|
-
```typescript
|
|
161
|
-
interface CacheInfo {
|
|
162
|
-
/** The implementation type in format "<category>/<implementation>" */
|
|
163
|
-
implementationType: string;
|
|
164
|
-
/** The eviction policy being used (if any) */
|
|
165
|
-
evictionPolicy?: string;
|
|
166
|
-
/** Default TTL in milliseconds (if configured) */
|
|
167
|
-
defaultTTL?: number;
|
|
168
|
-
/** Whether TTL is supported by this implementation */
|
|
169
|
-
supportsTTL: boolean;
|
|
170
|
-
/** Whether eviction is supported by this implementation */
|
|
171
|
-
supportsEviction: boolean;
|
|
172
|
-
}
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
### Getting Cache Information
|
|
176
|
-
|
|
177
|
-
```typescript
|
|
178
|
-
import { MemoryCacheMap, EnhancedMemoryCacheMap } from '@fjell/cache';
|
|
179
|
-
|
|
180
|
-
// Create cache instance
|
|
181
|
-
const cache = createCache(api, createCoordinate('item'), registry);
|
|
182
|
-
const cacheInfo = cache.getCacheInfo();
|
|
183
|
-
console.log(cacheInfo);
|
|
184
|
-
// Output: {
|
|
185
|
-
// implementationType: "memory/memory",
|
|
186
|
-
// supportsTTL: true,
|
|
187
|
-
// supportsEviction: false
|
|
188
|
-
// }
|
|
189
|
-
|
|
190
|
-
// Enhanced memory cache with eviction
|
|
191
|
-
const enhancedCache = createCache(api, createCoordinate('item'), registry, {
|
|
192
|
-
cacheType: 'memory',
|
|
193
|
-
memoryConfig: {
|
|
194
|
-
size: {
|
|
195
|
-
maxItems: 1000,
|
|
196
|
-
evictionPolicy: 'lru'
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
const enhancedInfo = enhancedCache.getCacheInfo();
|
|
201
|
-
console.log(enhancedInfo);
|
|
202
|
-
// Output: {
|
|
203
|
-
// implementationType: "memory/enhanced",
|
|
204
|
-
// evictionPolicy: "lru",
|
|
205
|
-
// supportsTTL: true,
|
|
206
|
-
// supportsEviction: true
|
|
207
|
-
// }
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
### Implementation Types
|
|
211
|
-
|
|
212
|
-
Each cache implementation has a unique type identifier:
|
|
213
|
-
|
|
214
|
-
- `memory/memory` - Basic MemoryCacheMap
|
|
215
|
-
- `memory/enhanced` - EnhancedMemoryCacheMap with eviction support
|
|
216
|
-
- `browser/localStorage` - LocalStorageCacheMap
|
|
217
|
-
- `browser/sessionStorage` - SessionStorageCacheMap
|
|
218
|
-
- `browser/indexedDB` - IndexDBCacheMap
|
|
219
|
-
|
|
220
|
-
### Capability Detection
|
|
221
|
-
|
|
222
|
-
Use the capability flags to adapt your application behavior:
|
|
223
|
-
|
|
224
|
-
```typescript
|
|
225
|
-
function configureCache<V extends Item<S>, S extends string>(
|
|
226
|
-
cache: Cache<V, S>
|
|
227
|
-
) {
|
|
228
|
-
const info = cache.getCacheInfo();
|
|
229
|
-
|
|
230
|
-
// Adjust TTL strategy based on support
|
|
231
|
-
if (info.supportsTTL) {
|
|
232
|
-
console.log(`TTL operations supported for ${info.implementationType}`);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// Show eviction policy if supported
|
|
236
|
-
if (info.supportsEviction && info.evictionPolicy) {
|
|
237
|
-
console.log(`Using ${info.evictionPolicy} eviction policy`);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// Log implementation details
|
|
241
|
-
console.log(`Cache implementation: ${info.implementationType}`);
|
|
242
|
-
}
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
### Display Information for Debugging
|
|
246
|
-
|
|
247
|
-
The cache info is particularly useful for debugging and monitoring:
|
|
248
|
-
|
|
249
|
-
```typescript
|
|
250
|
-
function debugCacheStatus<V extends Item<S>, S extends string>(
|
|
251
|
-
cacheName: string,
|
|
252
|
-
cache: Cache<V, S>
|
|
253
|
-
) {
|
|
254
|
-
const info = cache.getCacheInfo();
|
|
255
|
-
|
|
256
|
-
console.log(`[${cacheName}] Cache Configuration:`);
|
|
257
|
-
console.log(` Type: ${info.implementationType}`);
|
|
258
|
-
console.log(` TTL Support: ${info.supportsTTL ? 'Yes' : 'No'}`);
|
|
259
|
-
console.log(` Eviction Support: ${info.supportsEviction ? 'Yes' : 'No'}`);
|
|
260
|
-
|
|
261
|
-
if (info.evictionPolicy) {
|
|
262
|
-
console.log(` Eviction Policy: ${info.evictionPolicy}`);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (info.defaultTTL) {
|
|
266
|
-
console.log(` Default TTL: ${info.defaultTTL}ms`);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
## Common Patterns
|
|
272
|
-
|
|
273
|
-
### Factory Pattern
|
|
274
|
-
```typescript
|
|
275
|
-
import { CacheMap, MemoryCacheMap, LocalStorageCacheMap } from '@fjell/cache';
|
|
276
|
-
|
|
277
|
-
function createCacheMap<V extends Item<S>, S extends string>(
|
|
278
|
-
keyTypeArray: AllItemTypeArrays<S>,
|
|
279
|
-
environment: 'node' | 'browser-persistent' | 'browser-session' = 'node'
|
|
280
|
-
): CacheMap<V, S> {
|
|
281
|
-
switch (environment) {
|
|
282
|
-
case 'node':
|
|
283
|
-
return new MemoryCacheMap<V, S>(keyTypeArray);
|
|
284
|
-
case 'browser-persistent':
|
|
285
|
-
return new LocalStorageCacheMap<V, S>(keyTypeArray);
|
|
286
|
-
case 'browser-session':
|
|
287
|
-
return new SessionStorageCacheMap<V, S>(keyTypeArray);
|
|
288
|
-
default:
|
|
289
|
-
return new MemoryCacheMap<V, S>(keyTypeArray);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
### Error Handling for Storage
|
|
295
|
-
```typescript
|
|
296
|
-
try {
|
|
297
|
-
await cache.set(key, value);
|
|
298
|
-
} catch (error) {
|
|
299
|
-
if (error.message.includes('quota')) {
|
|
300
|
-
// Handle storage quota exceeded
|
|
301
|
-
cache.clear(); // Clear old data
|
|
302
|
-
await cache.set(key, value); // Retry
|
|
303
|
-
}
|
|
304
|
-
throw error;
|
|
305
|
-
}
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
## Key Normalization
|
|
309
|
-
|
|
310
|
-
All implementations use the same key normalization logic:
|
|
311
|
-
- String and number primary/location keys are normalized to strings
|
|
312
|
-
- Ensures consistent behavior across different implementations
|
|
313
|
-
- Prevents issues with mixed key types (e.g., '123' vs 123)
|
|
314
|
-
|
|
315
|
-
This refactoring maintains full backward compatibility while providing flexible storage options for different environments and use cases.
|