#Nest.js caching with Redis: command not found

1 messages · Page 1 of 1 (latest)

wicked wadi
#

package.json

{
  ...
  "scripts": {
    ...
  },
  "dependencies": {
    "@nestjs/cache-manager": "^2.2.0",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.1.1",
    "@nestjs/core": "^10.0.0",
    "cache-manager": "^5.4.0",
    "cache-manager-redis-yet": "^4.1.2",
    "redis": "^4.6.13"
  },
  "devDependencies": {
    ...
  }
}
#

general.module.ts

import { Module } from "@nestjs/common";
import { ConfigModule } from "./config.module";
import { ConfigService } from "src/config";
import { CacheService } from "src/use-case";
import { HealthController } from "src/delivery/http/health/health.controller";
import { CacheModule } from "@nestjs/cache-manager";
import { RedisClientOptions } from "redis";
import { redisStore } from 'cache-manager-redis-yet';

@Module({
    imports: [
        CacheModule.registerAsync<RedisClientOptions>({
            imports: [ConfigModule],
            inject: [ConfigService],
            useFactory: async (configService: ConfigService) => ({
                ...configService.getRedisConfig(),
                store: redisStore
            }),
            isGlobal: true
        })
    ],
    controllers: [HealthController],
    providers: [
        CacheService
    ]
})
export class GeneralModule { }
#

cache.service.ts

import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { Cache } from 'cache-manager';

@Injectable()
export class CacheService {
    protected readonly logger: Logger = new Logger(CacheService.name);

    constructor(@Inject(CACHE_MANAGER) protected cacheManager: Cache) { }

    async exists(key: string): Promise<boolean> {
        try {
            const result = await (this.cacheManager as any).exists(key); // doesn't work
            const result = await this.cacheManager.store.exists(key); // doesn't work
            return result === 1;
        } catch (error) {
            this.logger.error(`Error checking existence of key ${key}: ${error.message}`);
            return false;
        }
    }
}

Please help. Thank You.