What is @nestjs/cache-manager?
@nestjs/cache-manager is a caching module for the NestJS framework that provides a simple and efficient way to manage caching in your application. It supports various caching stores and strategies, making it flexible and powerful for different use cases.
What are @nestjs/cache-manager's main functionalities?
Basic Caching
This feature allows you to set up basic caching in your NestJS application. You can configure the time-to-live (ttl) and the maximum number of items in the cache.
const { CacheModule } = require('@nestjs/cache-manager');
const { Module } = require('@nestjs/common');
@Module({
imports: [
CacheModule.register({
ttl: 5, // seconds
max: 10, // maximum number of items in cache
}),
],
})
export class AppModule {}
Using Cache Interceptors
This feature demonstrates how to use cache interceptors to automatically cache the response of a controller method. This can be useful for reducing the load on your server by caching frequently requested data.
const { CacheInterceptor, CacheModule } = require('@nestjs/cache-manager');
const { Controller, Get, UseInterceptors } = require('@nestjs/common');
@Controller('cats')
@UseInterceptors(CacheInterceptor)
export class CatsController {
@Get()
findAll() {
return [{ name: 'Tom' }, { name: 'Jerry' }];
}
}
Custom Cache Store
This feature allows you to use a custom cache store, such as Redis, for your caching needs. This can be useful for distributed caching scenarios where you need a more robust and scalable caching solution.
const { CacheModule } = require('@nestjs/cache-manager');
const { Module } = require('@nestjs/common');
const * as redisStore from 'cache-manager-redis-store';
@Module({
imports: [
CacheModule.register({
store: redisStore,
host: 'localhost',
port: 6379,
}),
],
})
export class AppModule {}
Other packages similar to @nestjs/cache-manager
cache-manager
cache-manager is a popular caching library for Node.js that supports multiple storage engines, including in-memory, Redis, and more. It provides a flexible and powerful API for managing cache, similar to @nestjs/cache-manager, but it is not specific to the NestJS framework.
node-cache
node-cache is a simple and efficient in-memory caching solution for Node.js. It is easy to use and provides basic caching functionalities. However, it lacks the advanced features and integrations provided by @nestjs/cache-manager.
redis
redis is a powerful in-memory data structure store that can be used as a cache, database, and message broker. While it is not a caching library per se, it is often used in conjunction with caching libraries like @nestjs/cache-manager to provide a robust caching solution.
A progressive Node.js framework for building efficient and scalable server-side applications.
Description
cache-manager module for Nest originally published as part of the @nestjs/common
package. This package is a drop-in replacement for the deprecated CacheModule
.
Installation
$ npm i --save @nestjs/cache-manager cache-manager
Quick Start
Overview & Tutorial
Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Stay in touch
License
Nest is MIT licensed.