cache-panda
Version:
๐ผ A flexible, decorator-based smart cache module for NestJS with TTL, key prefixing, and conditional caching based on method execution time.
182 lines (127 loc) โข 4.62 kB
Markdown
# cache-panda
A smart, flexible cache module for **NestJS** ๐ผ that wraps `@nestjs/cache-manager` and introduces a flexible ` @CachePandaSet` decorator for conditional, granular, and performance-aware caching.
## ๐ฆ Installation
```bash
npm install cache-panda
```
Also install peer dependencies (if not already):
```bash
npm install @nestjs/cache-manager cache-manager
```
## ๐ Features
- โ
**Drop-in replacement** for `@nestjs/cache-manager`
- ๐ง Decorator-based `@CachePandaSet()` for elegant method-level caching
- โฑ๏ธ **Conditional caching** based on method execution time
- ๐ Custom cache key building with prefixes and argument targeting
- ๐งฉ Plugs into Redis, memory store, or any cache-manager compatible store
- ๐ ๏ธ Easy to use, fully typed, developer-friendly
## ๐ง Setup
### Register the module in your NestJS app:
```ts
// app.module.ts
import { CachePanda } from 'cache-panda';
@Module({
imports: [
CachePanda.register({
ttl: 60000, // Default TTL in ms
isGlobal: true, // Optional: make available globally
max: 1000, // Optional max number of keys
}),
],
})
export class AppModule {}
```
## ๐ง Usage
### Decorate your methods using `@CachePandaSet()`:
```ts
import { Injectable } from '@nestjs/common';
import { CachePandaSet } from 'cache-panda';
@Injectable()
export class MyService {
@CachePandaSet({
prefix: 'user:',
name: 'get-profile',
ttl: 12000,
argsIndex: [0],
executionTimeLimit: 20
})
async getUserProfile(userId: string) {
return { id: userId, name: 'Akhil' };
}
}
```
## ๐ `@CachePandaSet` Options
| Option | Type | Description |
|-----------------------|------------|---------------------------------------------------------------------|
| `prefix` | `string` | Key prefix to categorize/group caches |
| `name?` | `string` | Logical name for the method being cached |
| `ttl?` | `number` | Optional override for TTL (in ms) |
| `argsIndex?` | `number[]` | Pick specific method arguments to build the cache key |
| `executionTimeLimit?` | `number` | Only cache if method takes more than this number of ms |
## ๐ง Redis Setup
Also install peer dependencies (if not already):
```bash
npm install cache-manager-redis-yet
```
### Register the module in your NestJS app:
```ts
// app.module.ts
import { CachePanda } from 'cache-panda';
import { redisStore } from 'cache-manager-redis-yet';
@Module({
imports: [
CachePanda.registerAsync({
isGlobal: true,
useFactory: async () => ({
store: await redisStore({
url: 'redis://localhost:6379',
}),
ttl: 1000, // Default TTL for all cache entries in ms
max: 100, // Max items to keep in memory
}),
}),
],
})
export class AppModule {}
```
## ๐งผ Manual Cache Usage
```ts
await this.cachingService.setCache('some-key', JSON.stringify(data), 60);
const result = await this.cachingService.getCache('some-key');
await this.cachingService.deleteCache('some-key');
await this.cachingService.clearCache();
```
Advanced:
```ts
await this.cachingService.deleteCacheByKeys(['key1', 'key2']);
await this.cachingService.deleteCacheByKeyPattern('user:');
const keys = await this.cachingService.getKeysByKeyPattern('user:');
```
## ๐ Roadmap
- [x] Initial release with decorator + service
- [x] TTL and execution-time-based conditional caching
- [x] Redis and multi-store setup guide
- [ ] Auto-Invalidation (Cache Busting) decorator
- [ ] Logging and debug mode support (cache hit vs miss)
- [ ] CLI tool to inspect and manage cache keys
- [ ] 100% test cases coverage
## ๐ Contributing
Contributions are welcome! Open an issue or PR.
### Steps:
1. Fork the repo ๐ด
2. Create your feature branch (`git checkout -b feat/your-feature`)
3. Commit your changes (`git commit -am 'feat: add amazing thing'`)
4. Push to the branch (`git push origin feat/your-feature`)
5. Create a new Pull Request!
## ๐ License
MIT ยฉ [Akhil Kumar](https://github.com/d-akhil-kumar)
> Designed with โค๏ธ to make NestJS caching smarter, cleaner, and panda-fied.