Skip to content

Commit

Permalink
Merge pull request #10368 from nestjs/revert-10360-fix/cache-v5
Browse files Browse the repository at this point in the history
Revert "fix: update factory to account for old v4 syntax for cache stores"
  • Loading branch information
kamilmysliwiec committed Oct 5, 2022
2 parents ea61e29 + 530af92 commit ca8e48b
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions packages/common/cache/cache.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,32 @@ import { CacheManagerOptions } from './interfaces/cache-manager.interface';
export function createCacheManager(): Provider {
return {
provide: CACHE_MANAGER,
useFactory: async (options: CacheManagerOptions) => {
useFactory: (options: CacheManagerOptions) => {
const cacheManager = loadPackage('cache-manager', 'CacheModule', () =>
require('cache-manager'),
);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const cacheManagerVersion = require('cache-manager/package.json').version;
const cacheManagerMajor = cacheManagerVersion.split('.')[0];
const cachingFactory = async (
const cachingFactory = (
store: CacheManagerOptions['store'],
options: Omit<CacheManagerOptions, 'store'>,
): Promise<Record<string, any>> => {
): Record<string, any> => {
if (cacheManagerMajor < 5) {
return cacheManager.caching({
...defaultCacheOptions,
...{ ...options, store },
});
}
let cache: string | Function = 'memory';
if (typeof store === 'object' && 'create' in store) {
cache = store.create;
} else if (typeof store === 'function') {
cache = store;
}
return cacheManager.caching(cache, {
return cacheManager.caching(store ?? 'memory', {
...defaultCacheOptions,
...options,
});
};

return Array.isArray(options)
? cacheManager.multiCaching(
await Promise.all(
options.map(option => cachingFactory(option.store, option)),
),
options.map(option => cachingFactory(options.store, option)),
)
: cachingFactory(options.store, options);
},
Expand Down

0 comments on commit ca8e48b

Please sign in to comment.