Skip to content

Repository files navigation

node-ts-cache

npmLicense: MITNode.js VersionTypeScript

Simple and extensible caching module for TypeScript/Node.js with decorator support.

Features

  • Decorator-based caching - Use @Cache, @SyncCache, and @MultiCache decorators for elegant caching
  • Multiple storage backends - Memory, File System, Redis, LRU Cache, and more
  • Flexible expiration strategies - TTL-based expiration with lazy or eager invalidation
  • Multi-tier caching - Chain multiple cache layers (e.g., local LRU + remote Redis)
  • TypeScript-first - Full type safety with comprehensive interfaces
  • ESM support - Modern ES modules with Node.js 18+

Quick Start

npm install @node-ts-cache/core
import{Cache,ExpirationStrategy,MemoryStorage}from'@node-ts-cache/core';constcacheStrategy=newExpirationStrategy(newMemoryStorage());classUserService{
@Cache(cacheStrategy,{ttl: 300})asyncgetUser(id: string): Promise<User>{// This result will be cached for 5 minutesreturnawaitfetchUserFromDatabase(id);}}

Packages

This is a monorepo containing the following packages:

Core Package

PackageVersionDescription
@node-ts-cache/corenpmCore caching module with decorators, strategies, and built-in storages

Storage Adapters

PackageVersionDescription
@node-ts-cache/redis-storagenpmRedis storage using redis package (v4.x)
@node-ts-cache/ioredis-storagenpmRedis storage using ioredis with compression support
@node-ts-cache/node-cache-storagenpmIn-memory cache using node-cache
@node-ts-cache/lru-storagenpmLRU cache with automatic eviction
@node-ts-cache/lru-redis-storagenpmTwo-tier caching (local LRU + remote Redis)
@node-ts-cache/elasticsearch-storagenpmElasticsearch storage for search-optimized caching
@node-ts-cache/memcached-storagenpmMemcached storage for distributed caching
@node-ts-cache/valkey-storagenpmValkey storage (Redis-compatible, open source)

Documentation

For detailed documentation, see the main package README.

Architecture Overview

┌─────────────────────────────────────────────────────────────────┐
│ Application │
├─────────────────────────────────────────────────────────────────┤
│ Decorators Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │
│ │ @Cache │ │ @SyncCache │ │ @MultiCache │ │
│ │ (async) │ │ (sync) │ │ (multi-tier/batch) │ │
│ └──────┬──────┘ └──────┬──────┘ └──────────┬──────────┘ │
├──────────┼────────────────┼────────────────────┼────────────────┤
│ └────────────────┼────────────────────┘ │
│ ▼ │
│ ┌────────────────┐ │
│ │ Strategy │ │
│ │ (Expiration) │ │
│ └───────┬────────┘ │
├──────────────────────────┼──────────────────────────────────────┤
│ ▼ │
│ ┌──────────────────────────────────────────────────────────────────────────────────┐ │
│ │ Storage Layer │ │
│ ├────────┬──────┬───────┬─────┬───────────┬───────────────┬────────────┬──────────┤ │
│ │ Memory │ FS │ Redis │ LRU │ LRU+Redis │ Elasticsearch │ Memcached │ Valkey │ │
│ └────────┴──────┴───────┴─────┴───────────┴───────────────┴────────────┴──────────┘ │
└─────────────────────────────────────────────────────────────────┘

Choosing a Storage

StorageTypeUse CaseFeatures
MemoryStorageSyncDevelopment, small datasetsZero config, bundled
FsJsonStorageAsyncPersistent local cacheFile-based, survives restarts
NodeCacheStorageSyncProduction single-instanceTTL support, multi-ops
LRUStorageSyncMemory-constrained appsAuto-eviction, size limits
RedisStorageAsyncDistributed systemsShared cache, redis v4
RedisIOStorageAsyncDistributed systemsCompression, modern ioredis
LRUWithRedisStorageAsyncHigh-performance distributedLocal + remote tiers
ElasticsearchStorageAsyncSearch-integrated cachingFull-text search, scalable
MemcachedStorageAsyncHigh-performance distributedSimple, fast, widely supported
ValkeyStorageAsyncDistributed systemsRedis-compatible, open source

Requirements

  • Node.js >= 18.0.0
  • TypeScript >= 5.0 (for decorator support)

Development

# Install dependencies
npm install
# Build all packages
npm run build
# Run tests
npm test

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE for details.

Credits

Originally created by hokify, now maintained by simllll.

About

Simple and extensible caching module supporting decorators

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages