@kasifraza/nestjs-redis-cache
v1.0.1
Published
Decorator-based Redis caching for NestJS with auto-invalidation and TTL
Maintainers
Readme
@kasifraza/nestjs-redis-cache
Decorator-based Redis caching for NestJS with auto-invalidation and TTL.
Installation
npm install @kasifraza/nestjs-redis-cache ioredisUsage
Module Registration
import { CacheModule } from '@kasifraza/nestjs-redis-cache';
@Module({
imports: [
CacheModule.register({
host: 'localhost',
port: 6379,
keyPrefix: 'app:',
}),
],
})
export class AppModule {}@Cacheable Decorator
import { Cacheable } from '@kasifraza/nestjs-redis-cache';
class UserService {
@Cacheable({ key: 'users', ttl: 300 })
async getUsers() {
return this.userRepo.find();
}
}@CacheInvalidate Decorator
import { CacheInvalidate } from '@kasifraza/nestjs-redis-cache';
class UserService {
@CacheInvalidate({ key: 'users' })
async createUser(dto: CreateUserDto) {
return this.userRepo.save(dto);
}
}CacheService
import { CacheService } from '@kasifraza/nestjs-redis-cache';
@Injectable()
class MyService {
constructor(private cache: CacheService) {}
async example() {
await this.cache.set('key', { data: 1 }, 60);
const val = await this.cache.get('key');
await this.cache.del('key');
await this.cache.flush();
}
}License
MIT
