@nathapp/nestjs-cache 2.1.0-rc.0 → 2.1.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.
- package/CLAUDE.md +149 -0
- package/README.md +330 -420
- package/TODO.md +56 -0
- package/dist/enum/strategy.enum.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/inteceptor/index.d.ts +1 -2
- package/dist/inteceptor/index.js +1 -2
- package/dist/inteceptor/index.js.map +1 -1
- package/dist/interceptor/cache-invalidate.interceptor.js.map +1 -0
- package/dist/{inteceptor → interceptor}/cache.interceptor.js +9 -3
- package/dist/interceptor/cache.interceptor.js.map +1 -0
- package/dist/interceptor/index.d.ts +2 -0
- package/dist/interceptor/index.js +19 -0
- package/dist/interceptor/index.js.map +1 -0
- package/dist/interface/cache-invalidate.interface.d.ts +8 -0
- package/dist/interface/enhanced-cache-options.interface.d.ts +16 -0
- package/dist/interface/health.interface.d.ts +19 -0
- package/dist/interface/health.interface.js +3 -0
- package/dist/interface/health.interface.js.map +1 -0
- package/dist/interface/index.d.ts +2 -0
- package/dist/interface/index.js +2 -0
- package/dist/interface/index.js.map +1 -1
- package/dist/interface/metrics.interface.d.ts +17 -0
- package/dist/interface/metrics.interface.js +3 -0
- package/dist/interface/metrics.interface.js.map +1 -0
- package/dist/module/cache-invalidate.provider.js +5 -0
- package/dist/module/cache-invalidate.provider.js.map +1 -1
- package/dist/module/cache-module.interface.d.ts +1 -0
- package/dist/module/cache.module.d.ts +2 -1
- package/dist/module/cache.module.js +7 -3
- package/dist/module/cache.module.js.map +1 -1
- package/dist/module/cache.provider.js +69 -1
- package/dist/module/cache.provider.js.map +1 -1
- package/dist/provider/cacheable.provider.d.ts +3 -0
- package/dist/provider/cacheable.provider.js +120 -35
- package/dist/provider/cacheable.provider.js.map +1 -1
- package/dist/service/cache-invalidate.factory.js.map +1 -1
- package/dist/service/cache-invalidate.manager.d.ts +4 -3
- package/dist/service/cache-invalidate.manager.js +8 -5
- package/dist/service/cache-invalidate.manager.js.map +1 -1
- package/dist/service/cache-manager.d.ts +39 -8
- package/dist/service/cache-manager.js +579 -71
- package/dist/service/cache-manager.js.map +1 -1
- package/dist/service/cache-provider.js +3 -6
- package/dist/service/cache-provider.js.map +1 -1
- package/dist/service/cache-tag-registry.d.ts +30 -0
- package/dist/service/cache-tag-registry.js +100 -0
- package/dist/service/cache-tag-registry.js.map +1 -0
- package/dist/service/index.d.ts +1 -0
- package/dist/service/index.js +1 -0
- package/dist/service/index.js.map +1 -1
- package/dist/service/redis-cache-invalidate.manager.d.ts +9 -1
- package/dist/service/redis-cache-invalidate.manager.js +107 -55
- package/dist/service/redis-cache-invalidate.manager.js.map +1 -1
- package/dist/service/redis-pattern-invalidation.js +4 -0
- package/dist/service/redis-pattern-invalidation.js.map +1 -1
- package/dist/utils/cache-key.utils.d.ts +3 -0
- package/dist/utils/cache-key.utils.js +34 -2
- package/dist/utils/cache-key.utils.js.map +1 -1
- package/dist/utils/config-validation.d.ts +2 -0
- package/dist/utils/config-validation.js +57 -0
- package/dist/utils/config-validation.js.map +1 -0
- package/dist/utils/deprecation.d.ts +1 -0
- package/dist/utils/deprecation.js +13 -2
- package/dist/utils/deprecation.js.map +1 -1
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/redis-uri.utils.d.ts +9 -0
- package/dist/utils/redis-uri.utils.js +62 -0
- package/dist/utils/redis-uri.utils.js.map +1 -0
- package/dist/utils/validation.d.ts +10 -0
- package/dist/utils/validation.js +39 -0
- package/dist/utils/validation.js.map +1 -0
- package/docker-compose.e2e.yml +16 -0
- package/docs/CACHEOBLESYNC-MIGRATION-PLAN.md +55 -0
- package/docs/CODE-REVIEW-2026-02-10-v2.md +134 -0
- package/docs/CODE-REVIEW-2026-02-10.md +142 -0
- package/docs/CODE_REVIEW.md +356 -0
- package/docs/FINAL-CODE-REVIEW-2026-02-10.md +312 -0
- package/docs/IMPLEMENTATION_PLAN.md +1045 -0
- package/docs/IMPLEMENTATION_PLAN_V2.md +201 -0
- package/docs/IMPLEMENTATION_PLAN_V3.md +169 -0
- package/docs/IMPLEMENTATION_TASKS.md +1090 -0
- package/docs/PHASE3_COVERAGE_TASKS.md +266 -0
- package/docs/TAG-BASED-INVALIDATION-PLAN.md +367 -0
- package/docs/UNIFIED-INVALIDATE-PLAN.md +229 -0
- package/package.json +11 -4
- package/dist/inteceptor/cache-invalidate.interceptor.js.map +0 -1
- package/dist/inteceptor/cache.interceptor.js.map +0 -1
- /package/dist/{inteceptor → interceptor}/cache-invalidate.interceptor.d.ts +0 -0
- /package/dist/{inteceptor → interceptor}/cache-invalidate.interceptor.js +0 -0
- /package/dist/{inteceptor → interceptor}/cache.interceptor.d.ts +0 -0
package/CLAUDE.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# @nathapp/nestjs-cache Development Guide
|
|
2
|
+
|
|
3
|
+
## Package Overview
|
|
4
|
+
|
|
5
|
+
NestJS caching module supporting both legacy (`@nestjs/cache-manager`) and enhanced (`cacheable`) modes with multi-tier caching capabilities.
|
|
6
|
+
|
|
7
|
+
## Architecture
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
lib/
|
|
11
|
+
├── constant/ # Injection tokens, defaults
|
|
12
|
+
├── decorator/ # @CacheReqOptions, @CacheInvalidate
|
|
13
|
+
├── enum/ # CacheStrategy, InvalidateStrategy, Transport
|
|
14
|
+
├── external/ # External type definitions (Kafka, Redis, RMQ)
|
|
15
|
+
├── inteceptor/ # CacheInterceptor, CacheInvalidateInterceptor
|
|
16
|
+
├── interface/ # Type definitions
|
|
17
|
+
├── module/ # CacheModule, providers
|
|
18
|
+
├── provider/ # Cacheable provider factory
|
|
19
|
+
├── service/ # CacheManager, CacheProvider, invalidation managers
|
|
20
|
+
└── utils/ # Key resolution, deprecation, validation
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Cache Strategies
|
|
24
|
+
|
|
25
|
+
| Strategy | L1 (Memory) | L2 (Redis) | Use Case |
|
|
26
|
+
|----------|-------------|------------|----------|
|
|
27
|
+
| `LEGACY` | Via cache-manager | Via cache-manager | Existing apps |
|
|
28
|
+
| `MEMORY` | ✅ LRU | ❌ | Single instance, dev |
|
|
29
|
+
| `DISTRIBUTED` | ❌ | ✅ | Multi-instance, shared |
|
|
30
|
+
| `MULTI_TIER` | ✅ LRU | ✅ | Optimal performance |
|
|
31
|
+
|
|
32
|
+
## Critical: Legacy Mode Preservation
|
|
33
|
+
|
|
34
|
+
**DO NOT break backward compatibility.**
|
|
35
|
+
|
|
36
|
+
1. Default behavior MUST remain `strategy: LEGACY`
|
|
37
|
+
2. All existing APIs MUST continue to work
|
|
38
|
+
3. Use `isEnhancedMode()` to guard enhanced-only features
|
|
39
|
+
4. Deprecate gracefully (warn, don't fail)
|
|
40
|
+
|
|
41
|
+
## Key Files
|
|
42
|
+
|
|
43
|
+
| File | Purpose |
|
|
44
|
+
|------|---------|
|
|
45
|
+
| `cache-manager.ts` | Main service, strategy switching |
|
|
46
|
+
| `cache-provider.ts` | Legacy cache-manager wrapper |
|
|
47
|
+
| `cacheable.provider.ts` | Enhanced mode factory |
|
|
48
|
+
| `redis-cache-invalidate.manager.ts` | Cross-instance invalidation |
|
|
49
|
+
| `redis-pattern-invalidation.ts` | SCAN-based bulk deletion |
|
|
50
|
+
|
|
51
|
+
## Common Patterns
|
|
52
|
+
|
|
53
|
+
### Adding New Features
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
// Guard enhanced-only features
|
|
57
|
+
async newFeature(): Promise<Result> {
|
|
58
|
+
if (this.useCacheable && this.cacheable) {
|
|
59
|
+
// Enhanced mode implementation
|
|
60
|
+
return this.enhancedImplementation();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Legacy fallback or warning
|
|
64
|
+
this.logger.warn('Feature X is only available in enhanced mode');
|
|
65
|
+
return this.legacyFallback();
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Cache Key Resolution
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { resolveCacheKey } from '../utils';
|
|
73
|
+
|
|
74
|
+
// Always resolve keys consistently
|
|
75
|
+
const key = resolveCacheKey(['user', id]); // Returns: "USER:123"
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### TTL Shorthand
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { parseTtl } from '../provider/cacheable.provider';
|
|
82
|
+
|
|
83
|
+
parseTtl('5m'); // 300000 (5 minutes)
|
|
84
|
+
parseTtl('1h'); // 3600000 (1 hour)
|
|
85
|
+
parseTtl('1d'); // 86400000 (1 day)
|
|
86
|
+
parseTtl(5000); // 5000 (passthrough)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Testing
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
# Run tests
|
|
93
|
+
npm test
|
|
94
|
+
|
|
95
|
+
# Run with coverage
|
|
96
|
+
npm run test:cov
|
|
97
|
+
|
|
98
|
+
# Watch mode
|
|
99
|
+
npm run test:watch
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Test File Conventions
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
test/
|
|
106
|
+
├── module/ # Module integration tests
|
|
107
|
+
├── service/ # Service unit tests
|
|
108
|
+
├── util/ # Utility tests
|
|
109
|
+
└── cacheable-*.spec.ts # Enhanced mode tests
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Safety Checklist
|
|
113
|
+
|
|
114
|
+
Before committing changes:
|
|
115
|
+
|
|
116
|
+
- [ ] All existing tests pass
|
|
117
|
+
- [ ] Legacy mode behavior unchanged
|
|
118
|
+
- [ ] New features have tests
|
|
119
|
+
- [ ] Memory leaks addressed (Maps cleared, listeners removed)
|
|
120
|
+
- [ ] Passwords/secrets not logged
|
|
121
|
+
- [ ] Error messages don't expose sensitive data
|
|
122
|
+
|
|
123
|
+
## Known Issues
|
|
124
|
+
|
|
125
|
+
1. **Jest Coverage**: Config points to `src/**` but code is in `lib/**`
|
|
126
|
+
2. **Memory Leak**: `warnedFeatures` Set has no size limit
|
|
127
|
+
3. **Memory Leak**: `RedisPatternInvalidation` created per call
|
|
128
|
+
4. **Security**: Cache keys not sanitized
|
|
129
|
+
|
|
130
|
+
See `docs/CODE_REVIEW.md` and `docs/IMPLEMENTATION_PLAN.md` for details.
|
|
131
|
+
|
|
132
|
+
## Dependencies
|
|
133
|
+
|
|
134
|
+
### Required
|
|
135
|
+
|
|
136
|
+
- `@nestjs/common`, `@nestjs/core` (^8-11)
|
|
137
|
+
- `@nestjs/cache-manager` (^3.0.0) - Legacy mode
|
|
138
|
+
- `cache-manager` (>=6) - Legacy mode
|
|
139
|
+
|
|
140
|
+
### Optional (Enhanced Mode)
|
|
141
|
+
|
|
142
|
+
- `cacheable` (^1.0.0)
|
|
143
|
+
- `@keyv/redis` (^3.0.0)
|
|
144
|
+
- `@qified/redis` (^1.0.0)
|
|
145
|
+
- `ioredis` - For invalidation broker
|
|
146
|
+
|
|
147
|
+
## Migration
|
|
148
|
+
|
|
149
|
+
See `MIGRATION.md` for step-by-step guide from legacy to enhanced mode.
|