-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
132 lines (117 loc) · 3.6 KB
/
index.ts
File metadata and controls
132 lines (117 loc) · 3.6 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const Cacher = require('./lib/cacher');
import {log} from "./lib/logger";
import {ICacher, ICacherInstance, ICacherOptions, IStats} from "./lib/types";
let cachers:ICacherInstance[] = [];
let defaultTtl:number = 0;
let defaultClone:boolean = true;
let defaultStoreUndefinedObjects:boolean = false;
let intervalId:NodeJS.Timer;
const getCachers = (): ICacherInstance[] => {
cachers = cachers.filter(cacher => { return cacher !== undefined && cacher !== null });
return cachers;
}
const clearCachers = (): void => {
getCachers().forEach(cacher => {
cacher.clear();
log(`cleared cacher - id: ${cacher.id}`);
});
}
const cacherWrapper: ICacher = {
create: (options?: ICacherOptions) => {
const cacherOptions: ICacherOptions = options || {} as ICacherOptions
if (cacherOptions.ttl === undefined || typeof cacherOptions.ttl !== 'number') {
cacherOptions.ttl = defaultTtl;
}
if (cacherOptions.clone === undefined || typeof cacherOptions.clone !== 'boolean') {
cacherOptions.clone = defaultClone;
}
if (cacherOptions.storeUndefinedObjects === undefined || typeof cacherOptions.storeUndefinedObjects !== 'boolean') {
cacherOptions.storeUndefinedObjects = defaultStoreUndefinedObjects;
}
const cacher: ICacherInstance = new Cacher.default(cacherOptions);
cachers.push(cacher);
log(`created new cacher - id: ${cacher.id}`);
return cacher;
},
clear: () => {
clearCachers();
return cacherWrapper;
},
stats: (): IStats[] => {
const stats:IStats[] = []
getCachers().forEach(cacher => {
stats.push({
id: cacher.id,
stats: cacher.stats()
});
});
log('Stats across all cachers');
return stats;
},
cacher: (id: string): ICacherInstance => {
let cacher = undefined;
let cachers = getCachers();
for (var i = 0; i < cachers.length; i++) {
let currentCacher = cachers[i];
if (currentCacher.id === id) {
cacher = currentCacher;
break;
}
}
log(`getting cacher - id: ${id}, found: ${cacher !== undefined && cacher !== null }`);
return cacher;
},
cachers: (): ICacherInstance[] => {
let cachers = getCachers();
let ids = cachers.map((cacher) => { return cacher.id; });
log('getting cachers - ids', { ids });
return cachers;
},
ttl: (ttl: number): ICacher => {
if (ttl !== undefined && typeof ttl === 'number') {
defaultTtl = ttl;
log(`Default ttl set to - ${defaultTtl}`);
}
return cacherWrapper;
},
clone: (clone: boolean): ICacher => {
if (clone !== undefined && typeof clone === 'boolean') {
defaultClone = clone;
}
return cacherWrapper;
},
storeUndefinedObjects: (storeUndefinedObjects: boolean): ICacher => {
if (storeUndefinedObjects !== undefined && typeof storeUndefinedObjects === 'boolean') {
defaultStoreUndefinedObjects = storeUndefinedObjects;
}
return cacherWrapper;
},
cleanup: (seconds: number): ICacher => {
if (intervalId) {
clearInterval(intervalId);
}
intervalId = setInterval(() => {
log(`cleaning up expired keys`);
let count = 0;
getCachers().forEach((cacher) => {
cacher.keys().forEach((key) => {
cacher.getExpiry(key);
});
});
log(`cleaning up expired keys complete - ${count} keys`);
}, seconds * 1000);
return cacherWrapper;
},
dispose: (): void => {
if (intervalId) {
clearInterval(intervalId);
intervalId = undefined;
}
clearCachers();
cachers = [];
},
getCleanupIntervalId: (): NodeJS.Timer => {
return intervalId;
}
};
export = cacherWrapper