@kittawat/smart-cache
v0.1.0
Published
Memory-first browser cache core with pluggable persistence, TTL, subscriptions, events, and hooks.
Downloads
90
Maintainers
Readme
Smart Cache
Memory-first browser cache core with pluggable persistence, TTL, subscriptions, events, and external behavior composition.
Docs
- API reference: docs/api.md
- Project summary: docs/super_cache_project_summary.md
Install
npm install @kittawat/smart-cacheQuick Start
import { createCache } from '@kittawat/smart-cache'
const cache = createCache({
driver: 'memory',
persistTo: 'localStorage',
namespace: 'demo',
persistStrategy: 'debounce',
defaultTtl: 5 * 60_000,
})
cache.set('user.profile', { id: 1, name: 'Kittawat' }, { ttl: 120_000 })
const profile = cache.get('user.profile')
cache.subscribe('user.profile', (value) => {
console.log('profile changed:', value)
})
cache.on('cache:set', (payload) => {
console.log(payload)
})Expire Soon
ถ้าต้องการทำ action ตอน key ใกล้หมดอายุ ให้ตั้ง expireSoonThresholdMs และใช้ onExpireSoon หรือ event cache:expire:soon
const cache = createCache({
defaultTtl: 60_000,
expireSoonThresholdMs: 5_000,
onExpireSoon: ({ key, remainingMs }) => {
console.log(`"${key}" will expire in ${remainingMs}ms`)
},
})
cache.on('cache:expire:soon', ({ key, remainingMs }) => {
console.log(key, remainingMs)
})API
createCache(config)cache.get(key)cache.set(key, value, policy?)cache.update(key, updater, policy?)cache.remove(key)cache.clear()cache.has(key)cache.flush(key?)cache.hydrate(key?)cache.inspect(key)cache.cleanup()cache.subscribe(key, listener)cache.on(eventName, listener)cache.with(hooks)
Full API reference: docs/api.md
Scripts
npm run dev
npm run build
npm run test
npm run benchmark
npm run checknpm run dev จะรัน demo flow แบบง่าย ๆ และพิมพ์ console.table ให้เห็นลำดับการทำงานของ set/get/update/flush/hydrate/remove พร้อม state ของ cache กับ persistence layer
