Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this.cacheManager.store.client is undefined #685

Closed
softzer0 opened this issue May 12, 2024 · 2 comments
Closed

this.cacheManager.store.client is undefined #685

softzer0 opened this issue May 12, 2024 · 2 comments
Labels

Comments

@softzer0
Copy link

softzer0 commented May 12, 2024

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.

@softzer0 softzer0 added the bug label May 12, 2024
@softzer0 softzer0 changed the title this.cacheManager.store.client is undefined Can't resolve dependency even though the module is being globally imported May 12, 2024
@softzer0 softzer0 changed the title Can't resolve dependency even though the module is being globally imported this.cacheManager.store.client is undefined May 12, 2024
@peng-huang-ch
Copy link
Contributor

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants