@nathapp/nestjs-cache 2.0.4 → 2.1.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/MIGRATION.md +263 -0
  2. package/README.md +576 -1
  3. package/dist/enum/strategy.enum.d.ts +6 -0
  4. package/dist/enum/strategy.enum.js +8 -1
  5. package/dist/enum/strategy.enum.js.map +1 -1
  6. package/dist/index.d.ts +2 -0
  7. package/dist/index.js +2 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/interface/enhanced-cache-options.interface.d.ts +29 -0
  10. package/dist/interface/enhanced-cache-options.interface.js +3 -0
  11. package/dist/interface/enhanced-cache-options.interface.js.map +1 -0
  12. package/dist/interface/index.d.ts +1 -0
  13. package/dist/interface/index.js +1 -0
  14. package/dist/interface/index.js.map +1 -1
  15. package/dist/module/cache-module.interface.d.ts +2 -1
  16. package/dist/module/cache.module.js +34 -3
  17. package/dist/module/cache.module.js.map +1 -1
  18. package/dist/module/cache.provider.js +4 -3
  19. package/dist/module/cache.provider.js.map +1 -1
  20. package/dist/provider/cacheable.provider.d.ts +8 -0
  21. package/dist/provider/cacheable.provider.js +128 -0
  22. package/dist/provider/cacheable.provider.js.map +1 -0
  23. package/dist/provider/index.d.ts +1 -0
  24. package/dist/provider/index.js +18 -0
  25. package/dist/provider/index.js.map +1 -0
  26. package/dist/service/cache-manager.d.ts +8 -3
  27. package/dist/service/cache-manager.js +71 -2
  28. package/dist/service/cache-manager.js.map +1 -1
  29. package/dist/service/index.d.ts +1 -0
  30. package/dist/service/index.js +1 -0
  31. package/dist/service/index.js.map +1 -1
  32. package/dist/service/redis-pattern-invalidation.d.ts +11 -0
  33. package/dist/service/redis-pattern-invalidation.js +103 -0
  34. package/dist/service/redis-pattern-invalidation.js.map +1 -0
  35. package/dist/utils/deprecation.d.ts +2 -0
  36. package/dist/utils/deprecation.js +22 -0
  37. package/dist/utils/deprecation.js.map +1 -0
  38. package/dist/utils/index.d.ts +1 -0
  39. package/dist/utils/index.js +1 -0
  40. package/dist/utils/index.js.map +1 -1
  41. package/package.json +20 -2
package/MIGRATION.md ADDED
@@ -0,0 +1,263 @@
1
+ # Migration Guide: cache-manager to cacheable
2
+
3
+ This guide explains how to migrate from the legacy `@nestjs/cache-manager` to the new `cacheable` package with **ZERO breaking changes**.
4
+
5
+ ## Overview
6
+
7
+ The migration introduces a new `strategy` option that allows you to choose between:
8
+ - `MEMORY` - In-memory LRU cache
9
+ - `DISTRIBUTED` - Redis/Valkey distributed cache
10
+ - `MULTI_TIER` - L1 (memory) + L2 (Redis) cache
11
+ - `LEGACY` - Existing @nestjs/cache-manager (default)
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ # For enhanced mode (MEMORY, DISTRIBUTED, or MULTI_TIER)
17
+ npm install cacheable @keyv/redis @qified/redis
18
+
19
+ # Legacy mode works without additional dependencies
20
+ ```
21
+
22
+ ## Usage Examples
23
+
24
+ ### 1. Legacy Mode (Default - No Breaking Changes)
25
+
26
+ ```typescript
27
+ import { CacheModule } from '@nathapp/nestjs-cache';
28
+
29
+ @Module({
30
+ imports: [
31
+ CacheModule.register({
32
+ // No strategy specified = LEGACY mode
33
+ // Works exactly as before with @nestjs/cache-manager
34
+ isGlobal: true,
35
+ }),
36
+ ],
37
+ })
38
+ export class AppModule {}
39
+ ```
40
+
41
+ ### 2. Memory Cache (New)
42
+
43
+ ```typescript
44
+ import { CacheModule, CacheStrategy } from '@nathapp/nestjs-cache';
45
+
46
+ @Module({
47
+ imports: [
48
+ CacheModule.register({
49
+ strategy: CacheStrategy.MEMORY,
50
+ memory: {
51
+ lruSize: 1000, // Max items in LRU cache
52
+ ttl: '1h', // Shorthand: '1m', '1h', '1d' or milliseconds
53
+ checkPeriod: 60000, // Check for expired items every 60s
54
+ },
55
+ isGlobal: true,
56
+ }),
57
+ ],
58
+ })
59
+ export class AppModule {}
60
+ ```
61
+
62
+ ### 3. Distributed Cache (Redis/Valkey)
63
+
64
+ ```typescript
65
+ import { CacheModule, CacheStrategy } from '@nathapp/nestjs-cache';
66
+
67
+ @Module({
68
+ imports: [
69
+ CacheModule.register({
70
+ strategy: CacheStrategy.DISTRIBUTED,
71
+ distributed: {
72
+ url: 'redis://localhost:6379/0', // Full URL
73
+ // OR use individual options:
74
+ // host: 'localhost',
75
+ // port: 6379,
76
+ // password: 'secret',
77
+ // db: 0,
78
+ ttl: '1h',
79
+ keyPrefix: 'myapp:',
80
+ },
81
+ isGlobal: true,
82
+ }),
83
+ ],
84
+ })
85
+ export class AppModule {}
86
+ ```
87
+
88
+ ### 4. Multi-Tier Cache (L1 + L2)
89
+
90
+ ```typescript
91
+ import { CacheModule, CacheStrategy } from '@nathapp/nestjs-cache';
92
+
93
+ @Module({
94
+ imports: [
95
+ CacheModule.register({
96
+ strategy: CacheStrategy.MULTI_TIER,
97
+ memory: {
98
+ lruSize: 500,
99
+ ttl: '5m', // L1 cache for 5 minutes
100
+ },
101
+ distributed: {
102
+ url: 'redis://localhost:6379/0',
103
+ ttl: '1h', // L2 cache for 1 hour
104
+ keyPrefix: 'myapp:',
105
+ },
106
+ nonBlocking: true, // Don't block on cache misses
107
+ refreshThreshold: 0.2, // Refresh when 20% TTL remaining
108
+ isGlobal: true,
109
+ }),
110
+ ],
111
+ })
112
+ export class AppModule {}
113
+ ```
114
+
115
+ ### 5. Async Configuration
116
+
117
+ ```typescript
118
+ import { CacheModule, CacheStrategy } from '@nathapp/nestjs-cache';
119
+ import { ConfigService } from '@nestjs/config';
120
+
121
+ @Module({
122
+ imports: [
123
+ CacheModule.registerAsync({
124
+ inject: [ConfigService],
125
+ useFactory: (config: ConfigService) => ({
126
+ strategy: config.get('CACHE_STRATEGY') as CacheStrategy,
127
+ distributed: {
128
+ url: config.get('REDIS_URL'),
129
+ ttl: '1h',
130
+ },
131
+ isGlobal: true,
132
+ }),
133
+ }),
134
+ ],
135
+ })
136
+ export class AppModule {}
137
+ ```
138
+
139
+ ## New Features
140
+
141
+ ### 1. Check Cache Mode
142
+
143
+ ```typescript
144
+ import { CacheManager } from '@nathapp/nestjs-cache';
145
+
146
+ @Injectable()
147
+ export class MyService {
148
+ constructor(private cacheManager: CacheManager) {}
149
+
150
+ async someMethod() {
151
+ if (this.cacheManager.isEnhancedMode()) {
152
+ console.log('Using cacheable (enhanced mode)');
153
+ } else {
154
+ console.log('Using cache-manager (legacy mode)');
155
+ }
156
+ }
157
+ }
158
+ ```
159
+
160
+ ### 2. Get Cache Statistics (Enhanced Mode Only)
161
+
162
+ ```typescript
163
+ const stats = await this.cacheManager.getStats();
164
+ console.log('Cache stats:', stats);
165
+ ```
166
+
167
+ ### 3. Efficient Pattern-Based Invalidation (Enhanced Mode)
168
+
169
+ ```typescript
170
+ // Old way (slow, uses KEYS command)
171
+ await this.cacheManager.invalidate(['user', '*'], true);
172
+
173
+ // New way (fast, uses SCAN command)
174
+ await this.cacheManager.invalidateByPattern('user:*');
175
+ ```
176
+
177
+ ## Breaking Changes
178
+
179
+ **NONE!** All existing code continues to work without modification. The new features are opt-in.
180
+
181
+ ## Migration Path
182
+
183
+ ### Step 1: Continue Using Legacy Mode
184
+ No action needed. Your existing code works as-is.
185
+
186
+ ### Step 2: Test Enhanced Mode in Development
187
+ ```typescript
188
+ CacheModule.register({
189
+ strategy: CacheStrategy.MEMORY, // Start with memory cache
190
+ memory: { lruSize: 1000, ttl: '1h' },
191
+ isGlobal: true,
192
+ })
193
+ ```
194
+
195
+ ### Step 3: Migrate to Distributed Cache
196
+ ```typescript
197
+ CacheModule.register({
198
+ strategy: CacheStrategy.DISTRIBUTED,
199
+ distributed: {
200
+ url: process.env.REDIS_URL,
201
+ ttl: '1h',
202
+ },
203
+ isGlobal: true,
204
+ })
205
+ ```
206
+
207
+ ### Step 4: Optimize with Multi-Tier Cache
208
+ ```typescript
209
+ CacheModule.register({
210
+ strategy: CacheStrategy.MULTI_TIER,
211
+ memory: { lruSize: 500, ttl: '5m' },
212
+ distributed: {
213
+ url: process.env.REDIS_URL,
214
+ ttl: '1h',
215
+ },
216
+ nonBlocking: true,
217
+ refreshThreshold: 0.2,
218
+ isGlobal: true,
219
+ })
220
+ ```
221
+
222
+ ## TTL Shorthand Notation
223
+
224
+ Enhanced mode supports convenient TTL shorthand:
225
+ - `'30s'` - 30 seconds
226
+ - `'5m'` - 5 minutes
227
+ - `'1h'` - 1 hour
228
+ - `'7d'` - 7 days
229
+ - `300000` - 300,000 milliseconds
230
+
231
+ ## Deprecation Warnings
232
+
233
+ The following options are deprecated (but still work):
234
+ - `invalidateOptions` - Use `strategy` with enhanced options instead
235
+
236
+ You'll see warnings in the logs when using deprecated options.
237
+
238
+ ## Performance Improvements
239
+
240
+ Enhanced mode (cacheable) provides:
241
+ - ✅ **L1/L2 tiered caching** - Fast memory cache with Redis backup
242
+ - ✅ **Non-blocking operations** - Don't wait for cache misses
243
+ - ✅ **Background refresh** - Refresh before expiration
244
+ - ✅ **Efficient SCAN-based invalidation** - Safe for production Redis
245
+ - ✅ **Better memory management** - LRU eviction built-in
246
+
247
+ ## Troubleshooting
248
+
249
+ ### "Cannot find module 'cacheable'"
250
+ Install the required dependencies:
251
+ ```bash
252
+ npm install cacheable @keyv/redis @qified/redis
253
+ ```
254
+
255
+ ### "Redis client not available for pattern invalidation"
256
+ Make sure you're using `DISTRIBUTED` or `MULTI_TIER` strategy with valid Redis connection.
257
+
258
+ ### Legacy mode still working?
259
+ If you don't specify a `strategy`, it defaults to `LEGACY` mode. This ensures backward compatibility.
260
+
261
+ ## Questions?
262
+
263
+ See the main README or create an issue if you have questions about the migration.