-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSegmentsCacheInMemory.ts
More file actions
58 lines (42 loc) · 1.5 KB
/
SegmentsCacheInMemory.ts
File metadata and controls
58 lines (42 loc) · 1.5 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
import { isIntegerNumber } from '../../utils/lang';
import { ISegmentsCacheSync } from '../types';
/**
* Default ISplitsCacheSync implementation for server-side that stores segments definitions in memory.
*/
export class SegmentsCacheInMemory implements ISegmentsCacheSync {
private segmentCache: Record<string, Set<string>> = {};
private segmentChangeNumber: Record<string, number> = {};
update(name: string, addedKeys: string[], removedKeys: string[], changeNumber: number) {
const keySet = this.segmentCache[name] || new Set<string>();
addedKeys.forEach(k => keySet.add(k));
removedKeys.forEach(k => keySet.delete(k));
this.segmentCache[name] = keySet;
this.segmentChangeNumber[name] = changeNumber;
return addedKeys.length > 0 || removedKeys.length > 0;
}
isInSegment(name: string, key: string): boolean {
const segmentValues = this.segmentCache[name];
if (segmentValues) {
return segmentValues.has(key);
}
return false;
}
clear() {
this.segmentCache = {};
this.segmentChangeNumber = {};
}
getSegmentsCount() {
return Object.keys(this.segmentCache).length;
}
getKeysCount() {
return Object.keys(this.segmentCache).reduce((acum, segmentName) => {
return acum + this.segmentCache[segmentName].size;
}, 0);
}
getChangeNumber(name: string) {
const value = this.segmentChangeNumber[name];
return isIntegerNumber(value) ? value : undefined;
}
// No-op. Not used in server-side
resetSegments() { return false; }
}