Skip to content

Commit 94c9373

Browse files
committed
chore(deps): support cache-manager v6
1 parent df48080 commit 94c9373

11 files changed

+112
-39
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ module.exports = {
2121
'@typescript-eslint/no-explicit-any': 'off',
2222
'@typescript-eslint/no-use-before-define': 'off',
2323
'@typescript-eslint/no-non-null-assertion': 'off',
24+
'@typescript-eslint/no-require-imports': 'off',
2425
},
2526
};

lib/cache.module.ts

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { DynamicModule, Module } from '@nestjs/common';
2-
import type { Cache as CoreCache } from 'cache-manager'
2+
import { Events } from 'cache-manager';
33
import { CACHE_MANAGER } from './cache.constants';
44
import { ConfigurableModuleClass } from './cache.module-definition';
55
import { createCacheManager } from './cache.providers';
66
import {
77
CacheModuleAsyncOptions,
88
CacheModuleOptions,
99
} from './interfaces/cache-module.interface';
10+
import EventEmitter from 'node:events';
1011

1112
/**
1213
* This is just the same as the `Cache` interface from `cache-manager` but you can
@@ -15,7 +16,41 @@ import {
1516
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
1617
export abstract class Cache {}
1718
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
18-
export interface Cache extends CoreCache {}
19+
export interface Cache {
20+
get: <T>(key: string) => Promise<T | null>;
21+
mget: <T>(keys: string[]) => Promise<(Awaited<T> | null)[]>;
22+
set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
23+
mset: <T>(
24+
list: Array<{
25+
key: string;
26+
value: T;
27+
ttl?: number;
28+
}>,
29+
) => Promise<
30+
{
31+
key: string;
32+
value: T;
33+
ttl?: number;
34+
}[]
35+
>;
36+
del: (key: string) => Promise<boolean>;
37+
mdel: (keys: string[]) => Promise<boolean>;
38+
clear: () => Promise<boolean>;
39+
wrap: <T>(
40+
key: string,
41+
fnc: () => T | Promise<T>,
42+
ttl?: number | ((value: T) => number),
43+
refreshThreshold?: number,
44+
) => Promise<T>;
45+
on: <E extends keyof Events>(
46+
event: E,
47+
listener: Events[E],
48+
) => EventEmitter<[never]>;
49+
off: <E extends keyof Events>(
50+
event: E,
51+
listener: Events[E],
52+
) => EventEmitter<[never]>;
53+
}
1954

2055
/**
2156
* Module that provides Nest cache-manager.
@@ -30,7 +65,7 @@ export interface Cache extends CoreCache {}
3065
{
3166
provide: Cache,
3267
useExisting: CACHE_MANAGER,
33-
}
68+
},
3469
],
3570
exports: [CACHE_MANAGER, Cache],
3671
})

lib/cache.providers.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
CacheManagerOptions,
88
CacheStore,
99
} from './interfaces/cache-manager.interface';
10+
import { Keyv, KeyvStoreAdapter } from 'keyv';
1011

1112
/**
1213
* Creates a CacheManager Provider.
@@ -22,19 +23,41 @@ export function createCacheManager(): Provider {
2223
require('cache-manager'),
2324
);
2425
const cacheManagerIsv5OrGreater = 'memoryStore' in cacheManager;
26+
const cacheManagerIsv6OrGreater = 'KeyvAdapter' in cacheManager;
2527
const cachingFactory = async (
2628
store: CacheManagerOptions['store'],
2729
options: Omit<CacheManagerOptions, 'store'>,
2830
): Promise<Record<string, any>> => {
31+
if (cacheManagerIsv6OrGreater) {
32+
return store
33+
? cacheManager.createCache({
34+
stores: [
35+
new Keyv(
36+
{
37+
store,
38+
},
39+
{
40+
...defaultCacheOptions,
41+
...options,
42+
},
43+
),
44+
],
45+
})
46+
: cacheManager.createCache({
47+
...defaultCacheOptions,
48+
...options,
49+
});
50+
}
51+
2952
if (!cacheManagerIsv5OrGreater) {
3053
return cacheManager.caching({
3154
...defaultCacheOptions,
3255
...{ ...options, store },
3356
});
3457
}
3558

36-
// eslint-disable-next-line @typescript-eslint/ban-types
37-
let cache: string | Function | CacheStore = 'memory';
59+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
60+
let cache: string | Function | CacheStore | KeyvStoreAdapter = 'memory';
3861
defaultCacheOptions.ttl *= 1000;
3962
if (typeof store === 'object') {
4063
if ('create' in store) {

lib/interceptors/cache.interceptor.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class CacheInterceptor implements NestInterceptor {
3333
protected allowedMethods = ['GET'];
3434

3535
private cacheManagerIsv5OrGreater: boolean;
36+
private cacheManagerIsv6OrGreater: boolean;
3637

3738
constructor(
3839
@Inject(CACHE_MANAGER) protected readonly cacheManager: any,
@@ -46,6 +47,7 @@ export class CacheInterceptor implements NestInterceptor {
4647
() => require('cache-manager'),
4748
);
4849
this.cacheManagerIsv5OrGreater = 'memoryStore' in cacheManagerPackage;
50+
this.cacheManagerIsv6OrGreater = 'KeyvAdapter' in cacheManager;
4951
}
5052

5153
async intercept(
@@ -78,7 +80,11 @@ export class CacheInterceptor implements NestInterceptor {
7880

7981
const args = [key, response];
8082
if (!isNil(ttl)) {
81-
args.push(this.cacheManagerIsv5OrGreater ? ttl : { ttl });
83+
args.push(
84+
this.cacheManagerIsv5OrGreater || this.cacheManagerIsv6OrGreater
85+
? ttl
86+
: { ttl },
87+
);
8288
}
8389

8490
try {

lib/interfaces/cache-manager.interface.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { KeyvStoreAdapter } from 'keyv';
2+
13
export interface LiteralObject {
24
[key: string]: any;
35
}
@@ -70,7 +72,7 @@ export interface CacheManagerOptions {
7072
* [Different stores](https://docs.nestjs.com/techniques/caching#different-stores)
7173
* for more info.
7274
*/
73-
store?: string | CacheStoreFactory | CacheStore;
75+
store?: string | CacheStoreFactory | CacheStore | KeyvStoreAdapter;
7476
/**
7577
* Time to live - amount of time that a response is cached before it
7678
* is deleted. Subsequent request will call through the route handler and refresh
@@ -83,4 +85,8 @@ export interface CacheManagerOptions {
8385
*/
8486
max?: number;
8587
isCacheableValue?: (value: any) => boolean;
88+
/**
89+
* Add Cache Manager v6, Namespace for the current instance.
90+
*/
91+
namespace?: string;
8692
}

package-lock.json

+29-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"eslint-plugin-import": "2.30.0",
3838
"husky": "9.1.6",
3939
"jest": "29.7.0",
40+
"keyv": "^5.0.3",
4041
"lint-staged": "15.2.10",
4142
"prettier": "3.3.3",
4243
"reflect-metadata": "0.2.2",

tests/src/async-register-extra-providers/async-register-extra.controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Controller, Get, Inject } from '@nestjs/common';
2-
import { Cache } from 'cache-manager';
2+
import { Cache } from '../../../lib';
33
import { CACHE_MANAGER } from '../../../lib';
44

55
@Controller()

tests/src/async-register/async-register.controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Controller, Get, Inject } from '@nestjs/common';
2-
import { Cache } from 'cache-manager';
2+
import { Cache } from '../../../lib';
33
import { CACHE_MANAGER } from '../../../lib';
44

55
@Controller()

tests/src/default-ttl/default-ttl.controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Controller, Get, Inject } from '@nestjs/common';
2-
import { Cache } from 'cache-manager';
2+
import { Cache } from '../../../lib';
33
import { CACHE_MANAGER } from '../../../lib';
44

55
@Controller()

tests/src/multi-store/multi-store.controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Controller, Get, Inject } from '@nestjs/common';
2-
import { Cache } from 'cache-manager';
2+
import { Cache } from '../../../lib';
33
import { CACHE_MANAGER } from '../../../lib';
44

55
@Controller()

0 commit comments

Comments
 (0)