web / lib/services/cache/types
lib/services/cache/types
Type definitions for the generic caching system.
This module defines the core interfaces and types used throughout the cache implementation. The design is generic enough to support various storage backends (memory, filesystem, Redis, etc.) and different types of cached data.
Interfaces
CacheEntry<T>
Generic cache entry that can store any type of data
Type Parameters
T
T = unknown
Properties
key
key:
string
value
value:
T
metadata
metadata:
CacheEntryMetadata
CacheEntryMetadata
Metadata associated with a cache entry
Properties
createdAt
createdAt:
Date
expiresAt?
optionalexpiresAt:Date
accessCount
accessCount:
number
lastAccessedAt
lastAccessedAt:
Date
size?
optionalsize:number
tags?
optionaltags:string[]
custom?
optionalcustom:Record<string,unknown>
CacheSetOptions
Options for setting a cache value
Properties
ttl?
optionalttl:number
Time to live in seconds
tags?
optionaltags:string[]
Tags for grouping related entries
metadata?
optionalmetadata:Record<string,unknown>
Custom metadata
CacheStats
Cache statistics
Properties
entries
entries:
number
Number of entries in cache
totalSize
totalSize:
number
Total size in bytes
hits
hits:
number
Number of cache hits
misses
misses:
number
Number of cache misses
evictions
evictions:
number
Number of evictions
oldestEntry?
optionaloldestEntry:Date
Oldest entry timestamp
newestEntry?
optionalnewestEntry:Date
Newest entry timestamp
CacheStorage
Cache storage interface for different backends
Methods
get()
get<
T>(key):Promise<null|CacheEntry<T>>
Get a value from cache
Type Parameters
T
T = unknown
Parameters
key
string
Returns
Promise<null | CacheEntry<T>>
set()
set<
T>(key,value,options?):Promise<void>
Set a value in cache
Type Parameters
T
T = unknown
Parameters
key
string
value
T
options?
Returns
Promise<void>
delete()
delete(
key):Promise<boolean>
Delete a value from cache
Parameters
key
string
Returns
Promise<boolean>
has()
has(
key):Promise<boolean>
Check if key exists
Parameters
key
string
Returns
Promise<boolean>
clear()
clear(
pattern?):Promise<number>
Clear cache entries matching pattern
Parameters
pattern?
string
Returns
Promise<number>
Number of entries cleared
keys()
keys(
pattern?):Promise<string[]>
Get all keys matching pattern
Parameters
pattern?
string
Returns
Promise<string[]>
getMany()
getMany<
T>(keys):Promise<Map<string,CacheEntry<T>>>
Get multiple values at once
Type Parameters
T
T = unknown
Parameters
keys
string[]
Returns
Promise<Map<string, CacheEntry<T>>>
setMany()
setMany<
T>(entries,options?):Promise<void>
Set multiple values at once
Type Parameters
T
T = unknown
Parameters
entries
Map<string, T>
options?
Returns
Promise<void>
getStats()
getStats():
Promise<CacheStats>
Get cache statistics
Returns
Promise<CacheStats>
cleanup()
cleanup():
Promise<number>
Clean up expired entries
Returns
Promise<number>
Number of entries cleaned
destroy()?
optionaldestroy():void
Destroy the storage (cleanup resources)
Returns
void
Serializer
Serializer interface for converting objects to/from storable format
Methods
serialize()
serialize<
T>(value):string|Buffer<ArrayBufferLike>
Type Parameters
T
T
Parameters
value
T
Returns
string | Buffer<ArrayBufferLike>
deserialize()
deserialize<
T>(data):T
Type Parameters
T
T
Parameters
data
string | Buffer<ArrayBufferLike>
Returns
T
CacheConfig
Cache configuration
Properties
storage
storage:
CacheStorage
Storage backend
namespace?
optionalnamespace:string
Cache namespace for key prefixing
defaultTTL?
optionaldefaultTTL:number
Default TTL in seconds
maxSize?
optionalmaxSize:number
Maximum cache size in bytes
maxEntries?
optionalmaxEntries:number
Maximum number of entries
serializer?
optionalserializer:Serializer
Custom serializer
keyPrefix?
optionalkeyPrefix:string
Key prefix
onEviction()?
optionalonEviction: (key,value) =>void
Callback when entry is evicted
Parameters
key
string
value
unknown
Returns
void
MemoryCacheOptions
Options for memory cache storage
Properties
maxEntries?
optionalmaxEntries:number
Maximum number of entries
maxSize?
optionalmaxSize:number
Maximum total size in bytes
defaultTTL?
optionaldefaultTTL:number
Default TTL in seconds
onEviction()?
optionalonEviction: (key,entry) =>void
Callback when entry is evicted
Parameters
key
string
entry
Returns
void
FileSystemCacheOptions
Options for filesystem cache storage
Properties
cacheDir?
optionalcacheDir:string
Cache directory path
maxSize?
optionalmaxSize:number
Maximum cache size in bytes
cleanupIntervalMs?
optionalcleanupIntervalMs:number
Cleanup interval in milliseconds
defaultTTL?
optionaldefaultTTL:number
Default TTL in seconds
UrlFetchCacheEntry
URL fetch cache entry (for scheduled URL imports)
Properties
url
url:
string
method
method:
string
data
data:
Buffer
headers
headers:
Record<string,string>
statusCode
statusCode:
number
metadata
metadata:
UrlFetchCacheMetadata
UrlFetchCacheMetadata
URL fetch cache metadata
Properties
etag?
optionaletag:string
lastModified?
optionallastModified:string
expires?
optionalexpires:Date
maxAge?
optionalmaxAge:number
fetchedAt
fetchedAt:
Date
contentHash
contentHash:
string
size
size:
number
UrlFetchCacheOptions
Options for URL fetch cache
Properties
useCache?
optionaluseCache:boolean
Use cache for this request
bypassCache?
optionalbypassCache:boolean
Bypass cache and fetch fresh data
respectCacheControl?
optionalrespectCacheControl:boolean
Respect Cache-Control headers
forceRevalidate?
optionalforceRevalidate:boolean
Force revalidation
returnStaleOnError?
optionalreturnStaleOnError:boolean
Return stale cache on network error
cache?
optionalcache:"default"|"no-cache"|"force-cache"|"only-if-cached"
Cache mode (similar to fetch cache option)