-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (52 loc) · 2.21 KB
/
index.js
File metadata and controls
59 lines (52 loc) · 2.21 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
let _ = require('lodash');
let config = require('@cheevr/config').addDefaultConfig(__dirname, 'config');
/**
* @typedef {function} Callback
* @param {Error|string|null} [err]
* @param {...*} params
*/
/**
* @typedef {Object} CacheConfig
* @property {string} type The cache type that should be used to store data. Maps directly to the file names of the caches
* @property {number} ttl A time to live in seconds, that will be enacted by the Cache class if enabled, or by the implementation itself.
*/
class Manager {
constructor() {
this.reset();
config.on('change', () => this.reset());
}
/**
* Returns a cache implementation of the given type.
* @param {string} [name=_default_] The name of the cache instance (will be used to look up configs in files)
* @param {CacheConfig} [instanceConfig] The cache config that will be merged with the default options for this implementation
* @returns Cache
*/
instance(name = '_default_', instanceConfig) {
if (typeof name !== 'string') {
instanceConfig = name;
name = '_default_';
}
instanceConfig = instanceConfig || this._config[name] || { type: this._defaults.defaultType };
_.defaultsDeep(instanceConfig, this._defaults[instanceConfig.type]);
instanceConfig.type = instanceConfig.type.toLowerCase().trim();
return new (require('./' + instanceConfig.type))(instanceConfig, name);
}
/**
* Allows to add configuration to the current configuration. Any new configs will be merged with existing ones.
* @param {Object<string, CacheConfig>} config A map of cache instance names to cache configurations
* @param {Object<string, CacheConfig>} [defaults] A map of cache types to cache configurations
*/
configure(config, defaults) {
this._config = _.merge(this._config, config);
defaults && (this._defaults = _.merge(this._defaults, defaults));
}
/**
* Will reset the cache manager to the configuration found on the file system.
*/
reset() {
this._config = {};
this._defaults = {};
this.configure(config.cache, config.defaults.cache);
}
}
module.exports = new Manager();