@hile/cache
Build typed Redis read-through caches with TTL, tags, fieldable hashes, negative/stale options, and singleflight refreshes.Choose This Package When
Use When
Use these packages for database connections, Redis connections, structured logging, typed Redis caches, scheduled jobs, and file-system loaders.Do Not Use When
- Do not use default TypeORM or Redis services for multiple connections.
- Do not use
@hile/cachewithout returningnew Cache(...)from the loader function. - Do not use
@hile/scheduledistributed mode without Redis lock TTLs sized for job duration.
Install
Imports
Copy-Paste Example
More Examples
TypeORM transaction with compensation:Runtime And Lifecycle Notes
Cache(undefined)removes the key unless negative caching is configured.Cache#setExpire(seconds)uses seconds.defineCachetyped placeholders supportstring,number, andboolean.RedisCache.loadCache()returnsread,write,remove,has, andmulti.RedisCache.removeTag(tag)removes tagged cache entries.fieldablecaches use Redis hashes and cannot combine with stale or negative cache options.Scheduler.add()supports cron strings and{ delay }.Scheduler.load()reads default exports from*.schedule.*files produced bydefineJob().scanDirectory()matches.ts,.js,.tsx,.jsx, and.mjs.
Anti-Patterns
- Returning raw values from
defineCachehandlers instead ofnew Cache(value). - Treating cache as source of truth.
- Forgetting to destroy manually created Redis or TypeORM clients.
- Scheduling jobs without idempotency when side effects can repeat.
Verification Checklist
- Manual Redis clients call
disconnect()during cleanup. - Manual TypeORM data sources call
destroy()during cleanup. - Cache keys include app/tenant prefixes when shared Redis is used.
- Scheduled jobs have clear duplicate-run policy.
Related Recipes
Redis Cache With Singleflight
Complete Example
Package-Local AI Guide
This package also shipsAI.md in npm so agents can read accurate examples after installation.