cache-manager icon indicating copy to clipboard operation
cache-manager copied to clipboard

chore(deps): support cache-manager v6

Open zhx47 opened this issue 1 year ago • 2 comments
trafficstars

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • [ ] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • [ ] Bugfix
  • [x] Feature
  • [ ] Code style update (formatting, local variables)
  • [ ] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [ ] CI related changes
  • [ ] Other... Please describe:

What is the current behavior?

support cache-manager v6

Issue Number: xxx

What is the new behavior?

Does this PR introduce a breaking change?

  • [x] Yes
  • [ ] No

because cache-manager version 6 don't hava cache return type, upgraded need use import { Cache } from '@nestjs/cache-manager'; replace import { Cache } from 'cache-manager';

Other information

for example @keyv/redis

npm install cache-manager
npm install @keyv/redis

image image

zhx47 avatar Sep 26 '24 15:09 zhx47

What about, instead of supporting v5, v6 etc at once, we release a new major version of this package that supports only the latest version of cache-manager? Would you be interested in updating the PR to achieve that?

kamilmysliwiec avatar Sep 27 '24 07:09 kamilmysliwiec

What about, instead of supporting v5, v6 etc at once, we release a new major version of this package that supports only the latest version of cache-manager? Would you be interested in updating the PR to achieve that?

The previous PR has issues with multiple caches, cache-manager v6 is remove multiCaching function. I have submit a new commit to fix, and is only support cache-manager v6。 now, CacheModule configuration is like cache-manager. image image

zhx47 avatar Sep 27 '24 14:09 zhx47

@zhx47 - let me know if you need any help on this or additional features added to cache-manager v6.

jaredwray avatar Oct 16 '24 15:10 jaredwray

I don't want to be that guy but is there any stopper for this PR?

I need this new version and I can help if needed

Yhozen avatar Oct 22 '24 11:10 Yhozen

We're going to wait until the next major release as this upgrade introduces a breaking change

kamilmysliwiec avatar Oct 22 '24 11:10 kamilmysliwiec

@zhx47 - let me know if you need any help on this or additional features added to cache-manager v6.@zhx47 - 如果您在这方面需要任何帮助,或者需要为缓存管理器 v6 添加其他功能,请告诉我。

tks, everything is fine now

zhx47 avatar Oct 22 '24 14:10 zhx47

@kamilmysliwiec What is the timeline for that? Thank you in advance

pabacham avatar Oct 22 '24 14:10 pabacham

@kamilmysliwiec a beta of the next release would be a great way to test if something is broken

Yhozen avatar Oct 31 '24 18:10 Yhozen

onModuleDestroy should be added to cache instance to close possible redis connection: https://github.com/wmzy/nest-cache-manager/commit/5771200b729e7679e63d0a58831a8c662922c38d

wmzy avatar Nov 13 '24 08:11 wmzy

So many chaotic cache issues out there and the current version is broken with the latest cache-manager release we all know cache-manager-redis-yet is way too old and deprecated in favour of Keyv. Please merge and release sooner, this release would save a lot of stress

Johnvict avatar Nov 19 '24 09:11 Johnvict

I cherry-picked some of your changes and pushed them up https://github.com/nestjs/cache-manager/commit/a047deec6d06bbb8ce2beddc5046afe05d8784e7

kamilmysliwiec avatar Nov 22 '24 10:11 kamilmysliwiec

You can now test cache-manager v6 integration using the @nestjs/[email protected] version (published under the next tag). To install, run the following command:

npm i @nestjs/cache-manager@next

kamilmysliwiec avatar Nov 22 '24 10:11 kamilmysliwiec

npm i @nestjs/cache-manager@next keyv @keyv/redis
import { Cache } from '@nestjs/cache-manager';

import { createKeyv } from '@keyv/redis';

function getRedisUrl(configService: ConfigService) {
  const url = new URL('redis://');
  url.host = configService.getOrThrow('REDIS_HOST');
  url.port = configService.getOrThrow('REDIS_PORT');
  url.password = configService.getOrThrow('REDIS_PASSWORD');
  return url.toString();
}

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    CacheModule.registerAsync({
      useFactory: async (configService: ConfigService) => ({
        stores: [createKeyv(getRedisUrl(configService))],
      }),
      inject: [ConfigService],
      isGlobal: true,
    }),
    ....
})

zhdanovartur avatar Nov 26 '24 12:11 zhdanovartur

FROM NEST DOC- The Cache class is imported from the cache-manager, while CACHE_MANAGER token from the @nestjs/cache-manager package. (@Inject(CACHE_MANAGER) private cacheManager: Cache

Cache class is missing in cache manager in version 6 - What is the injectable class that i should use?

"@nestjs/cache-manager": "^3.0.0-next.0", "cache-manager": "^6.2.0",

nselladu avatar Nov 27 '24 15:11 nselladu

Cache class is missing in cache manager in version 6 - What is the injectable class that i should use?

import { type Cache, CACHE_MANAGER } from '@nestjs/cache-manager'

Just by looking at the code, Cache is a class (not just an interface/type) and is provided for dependency injection, so the injection token CACHE_MANAGER does not seem to be required anymore.

import { Cache } from '@nestjs/cache-manager';

class SomeService {
    constructor(private readonly cacheManager: Cache) {}
}

This is not really new and should work in both the stable version and the pre-release. Probably the docs need an update.

alumni avatar Nov 27 '24 15:11 alumni

Redis Namespace How can I target that in the cache manager's set or get method, given that there's no parameter to handle a specific namespace? https://keyv.org/docs/#5-advanced---use-namespaces-to-avoid-key-collisions

For Example. const session = new Keyv(new KeyvRedis(url), { namespace: 'session', useKeyPrefix: false, }); const dataservice = new Keyv(new KeyvRedis(url), { namespace: 'dataservice', useKeyPrefix: false, }); return { isGlobal: true, stores: [dataservice, session], };

class SomeService { constructor(private readonly cacheManager: Cache) {} } Is there's an existing way to handle this scenario? this.cacheManager.set(key, value, ttl, namespace)

nselladu avatar Nov 29 '24 18:11 nselladu

Redis Namespace How can I target that in the cache manager's set or get method, given that there's no parameter to handle a specific namespace? https://keyv.org/docs/#5-advanced---use-namespaces-to-avoid-key-collisions

For Example. const session = new Keyv(new KeyvRedis(url), { namespace: 'session', useKeyPrefix: false, }); const dataservice = new Keyv(new KeyvRedis(url), { namespace: 'dataservice', useKeyPrefix: false, }); return { isGlobal: true, stores: [dataservice, session], };

class SomeService { constructor(private readonly cacheManager: Cache) {} } Is there's an existing way to handle this scenario? this.cacheManager.set(key, value, ttl, namespace)

nestjs cache-manager is use cache-manager module, get method source code is here , is look like only support key. this module using multiple stores, and get tries to get a non-undefind value from all of them, which doesn't seem to match your needs.

set method does the same thing, you can understand that using multiple caches to prevent one cache from not working and causing problems? this module will set the value to all stores.

zhx47 avatar Nov 30 '24 11:11 zhx47

There's a small additional breaking change where the value returned by the default implementation for cacheManager.get is now null instead of undefined if no value is stored for that key.

RoopeHakulinen avatar Dec 04 '24 17:12 RoopeHakulinen

Cache class is missing in cache manager in version 6 - What is the injectable class that i should use?

import { type Cache, CACHE_MANAGER } from '@nestjs/cache-manager'

Just by looking at the code, Cache is a class (not just an interface/type) and is provided for dependency injection, so the injection token CACHE_MANAGER does not seem to be required anymore.

import { Cache } from '@nestjs/cache-manager';

class SomeService {
    constructor(private readonly cacheManager: Cache) {}
}

This is not really new and should work in both the stable version and the pre-release. Probably the docs need an update.

Or if we still using the type 'Cache' from cache-manager, @Inject(CACHE_MANAGER) is necessary

import { Cache } from 'cache-manager';
import { CACHE_MANAGER } from '@nestjs/cache-manager';

export class MyService {
  constructor(
      @Inject(CACHE_MANAGER)
      private readonly cacheManager: Cache,
  }
}

Correct me if anything wrong, thanks~

zhuxb711 avatar Dec 27 '24 10:12 zhuxb711

Or if we still using the type 'Cache' from cache-manager, @Inject(CACHE_MANAGER) is necessary

@zhuxb711 types are removed when transpiling from TS to JS, so they won't exist at runtime. You can't use them as injection tokens.

Cache from cache-manager is a type. Cache from @nestjs/cache-manager is a class and therefore it can be used as an injection token.

alumni avatar Dec 28 '24 08:12 alumni