Skip to content

MemoryCache

Defined in: packages/core/src/data/memory-cache.ts:104

TTL-based in-memory cache with LRU eviction.

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
const cache = new MemoryCache<GeoJSON.FeatureCollection>({
maxSize: 100,
defaultTTL: 300000, // 5 minutes
});
// Store data with ETag
cache.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 request
const headers = cache.getConditionalHeaders('https://example.com/data.json');
// headers = { 'If-None-Match': '"abc123"' }
Type ParameterDefault typeDescription
TunknownType of cached data

new MemoryCache<T>(config?): MemoryCache<T>

Defined in: packages/core/src/data/memory-cache.ts:121

Create a new MemoryCache instance

ParameterTypeDescription
config?Partial<CacheConfig>Cache configuration options

MemoryCache<T>

clear(): void

Defined in: packages/core/src/data/memory-cache.ts:210

Clear all cache entries and reset statistics

void


delete(key): boolean

Defined in: packages/core/src/data/memory-cache.ts:199

Delete a cache entry

ParameterTypeDescription
keystringCache key

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

ParameterTypeDescription
keystringCache key (typically a URL)

CacheEntry<T> | null

Cache entry or null if not found/expired

  • 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(key): Record<string, string>

Defined in: packages/core/src/data/memory-cache.ts:282

Get conditional request headers for HTTP caching

ParameterTypeDescription
keystringCache key

Record<string, string>

Object with conditional headers (may be empty)

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
const headers = cache.getConditionalHeaders(url);
const response = await fetch(url, { headers });
if (response.status === 304) {
// Use cached data
}

getStats(): CacheStats

Defined in: packages/core/src/data/memory-cache.ts:248

Get cache statistics

CacheStats

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)

ParameterTypeDescription
keystringCache key

boolean

True if key exists in cache


prune(): number

Defined in: packages/core/src/data/memory-cache.ts:225

Remove expired entries from cache

number

Number of entries removed

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

ParameterTypeDescription
keystringCache key (typically a URL)
entryCacheEntry<T>Cache entry to store

void

  • Evicts least recently used entries if at capacity
  • Updates access order

touch(key): void

Defined in: packages/core/src/data/memory-cache.ts:314

Update the last access time for an entry

ParameterTypeDescription
keystringCache key

void

Useful for keeping an entry “fresh” without modifying its data. Updates the access order for LRU.