-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.js
More file actions
54 lines (42 loc) · 1.09 KB
/
cache.js
File metadata and controls
54 lines (42 loc) · 1.09 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
var util = require("util");
var extend = util._extend;
var instances = {};
var defaultCacheInitConfig = {
name: '_default',
type: 'memory'
};
module.exports = {
init: function(config){
config = config || {};
config = extend(defaultCacheInitConfig, config);
var instance = initNewCacheInstance(config);
instances[config.name] = instance;
return instance;
},
retrieve: function(name){
if(!name){
name = '_default';
}
if(instances[name]){
return instances[name];
}
return null;
},
destroy: function(name){
if(!instances[name]){
return true;
}
instances[name].clear();
delete instances[name];
return true;
}
};
function initNewCacheInstance(config){
try{
var Instance = require('./storage/' + config.type);
return new Instance(config);
} catch(e){
console.log('initNewCacheInstance()', e, e.stack);
throw new Error(util.format('unknown cache storage %s', config.type));
}
}