forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.module.ts
More file actions
88 lines (84 loc) · 2.36 KB
/
Copy pathcache.module.ts
File metadata and controls
88 lines (84 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Module } from '../decorators';
import { DynamicModule, Provider } from '../interfaces';
import { CACHE_MANAGER, CACHE_MODULE_OPTIONS } from './cache.constants';
import { createCacheManager } from './cache.providers';
import {
CacheModuleAsyncOptions,
CacheModuleOptions,
CacheOptionsFactory,
} from './interfaces/cache-module.interface';
/**
* Module that provides Nest cache-manager.
*
* @see [Caching](https://docs.nestjs.com/techniques/caching)
*
* @publicApi
*/
@Module({
providers: [createCacheManager()],
exports: [CACHE_MANAGER],
})
export class CacheModule {
/**
* Configure the cache manager statically.
*
* @param options options to configure the cache manager
*
* @see [Customize caching](https://docs.nestjs.com/techniques/caching#customize-caching)
*/
static register(options: CacheModuleOptions = {}): DynamicModule {
return {
module: CacheModule,
providers: [{ provide: CACHE_MODULE_OPTIONS, useValue: options }],
};
}
/**
* Configure the cache manager dynamically.
*
* @param options method for dynamically supplying cache manager configuration
* options
*
* @see [Async configuration](https://docs.nestjs.com/techniques/caching#async-configuration)
*/
static registerAsync(options: CacheModuleAsyncOptions): DynamicModule {
return {
module: CacheModule,
imports: options.imports,
providers: [
...this.createAsyncProviders(options),
...(options.extraProviders || []),
],
};
}
private static createAsyncProviders(
options: CacheModuleAsyncOptions,
): Provider[] {
if (options.useExisting || options.useFactory) {
return [this.createAsyncOptionsProvider(options)];
}
return [
this.createAsyncOptionsProvider(options),
{
provide: options.useClass,
useClass: options.useClass,
},
];
}
private static createAsyncOptionsProvider(
options: CacheModuleAsyncOptions,
): Provider {
if (options.useFactory) {
return {
provide: CACHE_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
};
}
return {
provide: CACHE_MODULE_OPTIONS,
useFactory: async (optionsFactory: CacheOptionsFactory) =>
optionsFactory.createCacheOptions(),
inject: [options.useExisting || options.useClass],
};
}
}