MemoryCache
Defined in: packages/core/src/data/memory-cache.ts:104
TTL-based in-memory cache with LRU eviction.
Remarks
Section titled “Remarks”Features:
- LRU (Least Recently Used) eviction when at capacity
- Respects HTTP cache headers (ETag, Last-Modified)
- Configurable per-entry TTL
- Support for conditional requests (If-None-Match, If-Modified-Since)
- Automatic expiration checking on get operations
Example
Section titled “Example”const cache = new MemoryCache<GeoJSON.FeatureCollection>({ maxSize: 100, defaultTTL: 300000, // 5 minutes});
// Store data with ETagcache.set('https://example.com/data.json', { data: geojsonData, timestamp: Date.now(), etag: '"abc123"',});
// Retrieve (returns null if expired or missing)const entry = cache.get('https://example.com/data.json');if (entry) { console.log('Cache hit:', entry.data);}
// Get conditional headers for HTTP requestconst headers = cache.getConditionalHeaders('https://example.com/data.json');// headers = { 'If-None-Match': '"abc123"' }Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
T | unknown | Type of cached data |
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new MemoryCache<
T>(config?):MemoryCache<T>
Defined in: packages/core/src/data/memory-cache.ts:121
Create a new MemoryCache instance
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
config? | Partial<CacheConfig> | Cache configuration options |
Returns
Section titled “Returns”MemoryCache<T>
Methods
Section titled “Methods”clear()
Section titled “clear()”clear():
void
Defined in: packages/core/src/data/memory-cache.ts:210
Clear all cache entries and reset statistics
Returns
Section titled “Returns”void
delete()
Section titled “delete()”delete(
key):boolean
Defined in: packages/core/src/data/memory-cache.ts:199
Delete a cache entry
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | string | Cache key |
Returns
Section titled “Returns”boolean
True if entry was deleted, false if it didn’t exist
get(
key):CacheEntry<T> |null
Defined in: packages/core/src/data/memory-cache.ts:137
Retrieve a cache entry
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | string | Cache key (typically a URL) |
Returns
Section titled “Returns”CacheEntry<T> | null
Cache entry or null if not found/expired
Remarks
Section titled “Remarks”- Returns null if key doesn’t exist
- Returns null if entry has expired (and removes it)
- Updates access order for LRU
- Updates cache statistics
getConditionalHeaders()
Section titled “getConditionalHeaders()”getConditionalHeaders(
key):Record<string,string>
Defined in: packages/core/src/data/memory-cache.ts:282
Get conditional request headers for HTTP caching
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | string | Cache key |
Returns
Section titled “Returns”Record<string, string>
Object with conditional headers (may be empty)
Remarks
Section titled “Remarks”Returns appropriate If-None-Match and/or If-Modified-Since headers based on cached entry metadata. Returns empty object if:
- Key doesn’t exist in cache
- Entry has expired
- useConditionalRequests is false
Example
Section titled “Example”const headers = cache.getConditionalHeaders(url);const response = await fetch(url, { headers });if (response.status === 304) { // Use cached data}getStats()
Section titled “getStats()”getStats():
CacheStats
Defined in: packages/core/src/data/memory-cache.ts:248
Get cache statistics
Returns
Section titled “Returns”Current cache statistics including hit rate
has(
key):boolean
Defined in: packages/core/src/data/memory-cache.ts:166
Check if a key exists in cache (without checking expiration)
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | string | Cache key |
Returns
Section titled “Returns”boolean
True if key exists in cache
prune()
Section titled “prune()”prune():
number
Defined in: packages/core/src/data/memory-cache.ts:225
Remove expired entries from cache
Returns
Section titled “Returns”number
Number of entries removed
Remarks
Section titled “Remarks”Iterates through all entries and removes those that have exceeded their TTL. This is useful for periodic cleanup.
set(
key,entry):void
Defined in: packages/core/src/data/memory-cache.ts:180
Store a cache entry
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | string | Cache key (typically a URL) |
entry | CacheEntry<T> | Cache entry to store |
Returns
Section titled “Returns”void
Remarks
Section titled “Remarks”- Evicts least recently used entries if at capacity
- Updates access order
touch()
Section titled “touch()”touch(
key):void
Defined in: packages/core/src/data/memory-cache.ts:314
Update the last access time for an entry
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | string | Cache key |
Returns
Section titled “Returns”void
Remarks
Section titled “Remarks”Useful for keeping an entry “fresh” without modifying its data. Updates the access order for LRU.