nestjs-redis-connector
v1.0.4
Published
redis connector for NestJs based services
Downloads
554
Maintainers
Readme
nestjs-redis-connector
introduction
NestJS Redis Connector is a library that simplifies using Redis as a caching mechanism in your NestJS-based projects. This library provides an easy-to-use interface for connecting to a Redis server and interacting with it.
Badges
Installation
YYou can install the NestJS Redis Connector package using npm or yarn:
npm install nestjs-redis-connector
Usage/Examples
- Import the
RedisConnectorModule
In your NestJS application, you need to import theRedisConnectorModule
and register it with a connection configuration. This should be done in the application module, typicallyapp.module.ts
.
import { Module } from '@nestjs/common';
import { RedisConnectorModule } from 'nestjs-redis-connector';
@Module({
imports: [
RedisConnectorModule.register({
url: 'redis://localhost:6379', // Replace with your Redis server URL
ttl: 0, // Set your default TTL (time-to-live) for cached data
}),
],
})
export class AppModule {}
- Use
RedisClientService
Now that you have registered theRedisConnectorModule
, you can use theRedisClientService
in yourservices
orcontrollers
to interact with Redis.
import { Controller, Get } from '@nestjs/common';
import { RedisClientService } from 'nestjs-redis-connector';
@Controller('cache')
export class CacheController {
constructor(private readonly redisClient: RedisClientService) {}
@Get(':key')
async getCacheValue(@Param('key') key: string): Promise<string> {
const cachedValue = await this.redisClient.getValue(key);
return cachedValue;
}
}
In the example above, we've created a controller
that retrieves a cached value using the getValue
method of the RedisClientService
. You can customize your usage as per your application's requirements.
License
This package is released under the MIT License