Skip to content

Instantly share code, notes, and snippets.

@sergiycheck
Created July 2, 2022 10:33
Show Gist options
  • Save sergiycheck/89c959971fbfaf60208572c906025f31 to your computer and use it in GitHub Desktop.
Save sergiycheck/89c959971fbfaf60208572c906025f31 to your computer and use it in GitHub Desktop.
nest js redis cache dynamic global module
import { CacheModule, Global, Module } from '@nestjs/common';
import type { ClientOpts } from 'redis';
import { ConfigModule, ConfigService } from '@nestjs/config';
import redisStore from 'cache-manager-redis-store';
@Global()
@Module({
imports: [
CacheModule.registerAsync<ClientOpts>({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => {
const ttl = configService.get('CACHE_TTL'); //in seconds
const host = configService.get('REDIS_CACHE_HOST');
const port = configService.get('REDIS_CACHE_PORT');
return {
ttl,
max: 10,
store: redisStore,
host,
port,
};
},
inject: [ConfigService],
}),
],
exports: [CacheModule],
})
export class DynamicRedisCacheModule {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment