jaredwray / cache-manager

Cache module for Node.JS
MIT License
1.4k stars 152 forks source link

`this.cacheManager.store.client` is undefined #685

Closed softzer0 closed 2 months ago

softzer0 commented 2 months ago

Describe the bug I'm using the latest version of cache-manager-redis-yet from NPM which is 5.0.0, and I'm using it in a combination with NestJS. However, as the title says, I'm encountering an issue where I can't access the Redis client instance, even though I correctly define the cache manager using this wrapper.

This is what I see when I inspect this.cacheManager.store object:

calculatedSize:
ƒ calculatedSize() {\n            return lruCache.calculatedSize;\n        }
del:
ƒ async del(key) {\n            lruCache.delete(key);\n        }
dump:
() => lruCache.dump()
get:
async (key) => lruCache.get(key)
keys:
async () => [...lruCache.keys()]
load:
ƒ load(...arguments_) {\n            lruCache.load(...arguments_);\n        }

There's no client even though it should be there...

How To Reproduce Some simple example code of NestJS service:

import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { RedisCache } from "cache-manager-redis-yet";

export class RedisService {
  constructor(
    @Inject(CACHE_MANAGER)
    private readonly cacheManager: RedisCache
  ) {}

   private async getKeysFromKeySet(keySet: string): Promise<string[]> {
     const keys: string[] = await this.cacheManager.store.client.sMembers(keySet);
     return keys;
   }
}

Module:

import { CacheModule, Module } from '@nestjs/common';
import { RedisClientOptions } from 'redis';
import { redisStore } from 'cache-manager-redis-yet';
import { RedisService } from './redis.service';

@Module({
  imports: [
    CacheModule.register<RedisClientOptions>({
      isGlobal: true,
      store: redisStore,
      url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`,
    }),
  ],
  providers: [RedisService],
  exports: [RedisService],
})
export class RedisModule {}

EDIT: This might be a bogus issue, as I'm currently doing other checks and seems like the issue is deeper than I've initially thought.

peng-huang-ch commented 2 months ago

You should use the @nestjs/cache-manager, follow the caching doc, and replace cache-manager-redis-store with cache-manager-redis-yet

Module

// redis.module.ts
import { CacheModule } from '@nestjs/cache-manager';
import { Module } from '@nestjs/common';

// import * as redisStore from 'cache-manager-redis-store'; replace the redisStore
import { redisStore } from 'cache-manager-redis-yet'; // 

import { RedisService } from './redis.service';

@Module({
  imports: [
     CacheModule.register({
      isGlobal: true,
      store: redisStore,
      url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`,
    }),
  ],
  providers: [RedisService],
  exports: [RedisService],
})
export class RedisModule {}

Service

// redis.service.ts
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Inject } from '@nestjs/common';

import { Cache } from 'cache-manager';
import { RedisStore } from 'cache-manager-redis-yet';
import { RedisClientType } from 'redis';

export class RedisService {
  client: RedisClientType;
  constructor(
    @Inject(CACHE_MANAGER)
    private readonly cacheManager: Cache,
  ) {
    this.client = (this.cacheManager.store as RedisStore).client as RedisClientType;
  }

  async getKeysFromKeySet(keySet: string): Promise<string[]> {
    const keys: string[] = await this.client.sMembers(keySet);
    return keys;
  }
}
softzer0 commented 2 months ago

Yeah, this turned out to be the bogus issue in the end and it was some other problem which I managed to solve. My apologies, I totally forgot that I made this issue.