cache-manager: NestJS v9 incompatible with version 5

i get this error

TypeError: store.get is not a function
    at Object.get (.../node_modules/cache-manager/src/caching.ts:88:36)
    at AppController.getHello

Im implementing caching following the NestJS docs.

this is my posting on stackoverflow (https://stackoverflow.com/questions/73908197/typeerror-store-get-is-not-a-function-nestjs-cache-manager)

but when im switch to version4 then its working… but i dont know why.

i wonder this is just my fault or version issue

-> i try to find difference but i think get method’s logic is same(using lru-cache package). so maybe type issue?

if this is my fault, then can you tell me how to solve it?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 25
  • Comments: 35 (8 by maintainers)

Commits related to this issue

Most upvoted comments

For the ones using cache-manager-redis-store, you also need to downgrade it to v2.0.0 (instead of 3.0.1).

After some investigation, it appears the caching method’s signature has changed to now accept a factory as the first parameter and options as the second vs the old single options object that included the store. Nest will need to update some code to support v5, but I make no guarantees if we’ll concurrently support v4 and v5 or just v4 until Nest v10 is released.

This change was rather hard to find as the old lib/caching and the new src/caching have different git histories and there’s no changelog explicitly mentioning this change

If you are coming here from the NestJS caching Redis tutorial, use these dependencies:

"cache-manager": "4.1.0",
"cache-manager-redis-store": "2.0.0",

@vincentwinkel thanks

I managed to solve this with:

"@nestjs/common": "^9.4.0",
"cache-manager": "^5.2.0",
"cache-manager-redis-yet": "^4.1.1",

My module file:

import { redisStore } from 'cache-manager-redis-yet';
import { Module, Global } from '@nestjs/common';
import { CacheModule } from '@nestjs/cache-manager';
import { ConfigModule, ConfigService } from '@nestjs/config';

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

@Global()
@Module({
  imports: [
    CacheModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        store: await redisStore({
          url: configService.get('REDIS_URL'),
          database: 0,
        }),
      }),
    }),
  ],
  providers: [RedisService],
  exports: [RedisService],
})
export class RedisModule {}

My provider file:

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

@Injectable()
export class RedisService {
  constructor(@Inject(CACHE_MANAGER) private readonly cache: Cache) {}

  async get(key: any) {
    return await this.cache.get(key);
  }

  async set(key: any, value: any, ttl: number) {
    await this.cache.set(key, value, ttl * 1000); // from cache-manager v5 they use milliseconds instead of seconds
  }

  async del(key: any) {
    await this.cache.del(key);
  }
}

You are welcome.

My fix for NestJS was to downgrade to "cache-manager": "^4.0.0",.

This is also forced by @micalevisk 's PR here.

I’m no longer the maintainer of this project, but:

  1. The major version was revved from 4 to 5, which implies that there is (or could be) a breaking change.
  2. Users should specify a version (or at least a version range) in their projects to prevent breaking changes from causing problems.

But I also agree that the constructors should not be async.

Fixed in nest@9.2.0

Thanks all!!

I can confirm that using the latest NestJS version (9.2.1) solves this issue for me. Looks like anything north of 9.1.3 should address the issue, which is when this was merged: https://github.com/nestjs/nest/commit/4eb5fb00a3f945e73087aab0d578661c0939dbf6

@srinivasan-getstan there’s nothing really to update. botika was kind enough to provide an example of how to use cache-manager@^5 with Nest if you want to use that until a stable fix gets merged in with Nest’s code base. Otherwise, v5 s a breaking change and you should use v4 for the old functionality. ThisPR should fix any usage issues between v4 and v5, at least in the tests I’ve done when creating it, but we’ll have to wait for it to get merged in and published, which may take a bit. Until then, as I said, there’s nothing cache-manager needs to do about this, they correctly released a major change, which can include breaking changes, and that’s that.

+1 for a fix.

I believe this is the commit that broke everything. A “Breaking Changes” should have been issued.

same here…back to version @4.1.0 and wait

to resolve that i need to change the “cache-manager”: “^4.0.0”, and if you use redis “cache-manager-redis-store”: “2.0.0”, this resolved from me

Hi. This still does not work for me even after updating it to 9.2.0. Below is the code for initiating CacheModule in app.module.ts:

CacheModule.register<RedisClientOptions>({
     isGlobal: true,
     store: redisStore,
     host: 'localhost',
     port: 6379,
   }),

Versions: cache-manager: 5.1.6 redis:3.1.2 cache-manager-redis-store:3.0.1

@WaylonOKC as this was over a semver major version, a breaking change was fine. It’s just that until now, Nest’s peerDeps were okay with any version of node-cache-manager. Therefore, no “fix” is necessary from this library. I’ll be working on an update for Nest to figure out if support ing v4 and v5 simultaneously is possible

cache-manager-redis-store: "^3.0.1"

Is not updated. Use https://github.com/node-cache-manager/node-cache-manager#official-and-updated-to-last-version instead

The awser is in the last post. And install Linux, archlinux, https://wiki.archlinux.org/title/installation_guide

This is work for me. redis: “^3.1.2” cache-manager: “^5.1.3” cache-manager-redis-store: “^3.0.1”

 CacheModule.registerAsync<any>({
      isGlobal: true,
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        const redisConfig = configService.get<RedisConfig>('redis');
        const storeConfig: CacheModuleOptions<RedisClientOptions<RedisModules, RedisFunctions, RedisScripts>> = {
          url: `redis://${redisConfig.host}:6379`,
        };
        if (redisConfig.auth) {
          storeConfig.password = redisConfig.auth;
        }
        const store = await redisStore(storeConfig);
        return { store: () => store };
      },
    }),

It should be noted that this.cacheManager.set(key, value, { ttl } as any);

node-cache-manager version 5 does not work with Nest version 8 either

Hi Team, Any update?

Basically, I did https://github.com/node-cache-manager/node-cache-manager/commit/12020d8de62142d47c28dba0cf1f820328488663 and remove it. I would use version 4 until nest is updated or you can use that module. There are also not many updated Stores yet.

@WaylonOKC as this was over a semver major version, a breaking change was fine. It’s just that until now, Nest’s peerDeps were okay with any version of node-cache-manager. Therefore, no “fix” is necessary from this library. I’ll be working on an update for Nest to figure out if support ing v4 and v5 simultaneously is possible

Breaking changes are often expected by a major version, but they definitely should be called out and not just left to be discovered after people upgrade.

FWIW, this thread started out as a “TypeError” and not specifically a NestJS incompatibility.

same here