cache-manager
cache-manager copied to clipboard
chore(deps): support cache-manager v6
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
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?
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.
@zhx47 - let me know if you need any help on this or additional features added to cache-manager v6.
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
We're going to wait until the next major release as this upgrade introduces a breaking change
@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
@kamilmysliwiec What is the timeline for that? Thank you in advance
@kamilmysliwiec a beta of the next release would be a great way to test if something is broken
onModuleDestroy should be added to cache instance to close possible redis connection: https://github.com/wmzy/nest-cache-manager/commit/5771200b729e7679e63d0a58831a8c662922c38d
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
I cherry-picked some of your changes and pushed them up https://github.com/nestjs/cache-manager/commit/a047deec6d06bbb8ce2beddc5046afe05d8784e7
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
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,
}),
....
})
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",
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.
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)
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.
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.
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,
Cacheis a class (not just an interface/type) and is provided for dependency injection, so the injection tokenCACHE_MANAGERdoes 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~
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.