@acfatah/memory-cache
v0.1.7
Published
A simple in-memory cache for bun and nodejs.
Maintainers
Readme
Memory Cache
A simple in-memory cache for bun and nodejs.
Features
- No dependencies.
- Uses
Mapinstead of plain object. - Purges expired cache in an interval.
- Optional cache schema type inference.
Usage
import { useMemoryCache } from '@acfatah/memory-cache'
const { set, get, remove, keys, clear } = useMemoryCache({
// Default expiration value is 5 minutes if not provided.
ttl: 15 * 60 * 1000
})
// ...
set('cache-key', value)To set, get and remove cache,
// Set custom expiration
set('cache-key', value, { ttl: 5 * 60 * 1000 })
// Set a value with no expiration
set('cache-key', value, { ttl: null })
// or
set('cache-key', value, { ttl: Infinity })
// get a cache value
const value = get('cache-key')
// remove a cache value
remove('cache-key')
// or
set('cache-key', undefined)
// list all available keys
keys()
// clear all cache
clear()By default, the purge interval is set to 1 minute. To change it, use the setPurgeTimeout method. Note that this will affect all caches globally.
To cache schema type inference,
const { get, set, keys } = useMemoryCache<{
'cache-key-1': number
'cache-key-2': string
'cache-key-3': { foo: string }
}>()get method will infer all available keys and their types based on the cache schema.
