-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathcache.ts
More file actions
197 lines (153 loc) · 5.23 KB
/
cache.ts
File metadata and controls
197 lines (153 loc) · 5.23 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
export interface ICacheOptions {
// backend is expected to have the same static interface as AsyncStorage
backend: any;
namespace: string;
policy: ICachePolicy;
prunecallback? : ((keys : string[]) => void);
}
export interface ICachePolicy {
maxEntries: number;
stdTTL: number; // second
}
export default class Cache {
protected backend: any;
protected namespace: string;
protected policy: ICachePolicy;
protected prunecallback? : ((keys : string[]) => void);
constructor(options: ICacheOptions) {
this.namespace = options.namespace;
this.backend = options.backend;
this.policy = options.policy;
this.prunecallback = options.prunecallback;
let ttl = this.policy.stdTTL;
if (!ttl || typeof(ttl) !== 'number') {
ttl = 0;
}
this.policy.stdTTL = ttl;
}
public async clearAll() {
const keys = await this.backend.getAllKeys();
const namespaceKeys = keys.filter((key: string) => {
return key.substr(0, this.namespace.length) === this.namespace;
});
await this.backend.multiRemove(namespaceKeys);
return this.setLRU([]);
}
public async enforceLimits(): Promise<void> {
if (!this.policy.maxEntries) {
return;
}
const lru = await this.getLRU();
const victimCount = Math.max(0, lru.length - this.policy.maxEntries);
const victimList = lru.slice(0, victimCount);
const removePromises = [];
for (const victimKey of victimList) {
removePromises.push(this.remove(victimKey));
}
await Promise.all(removePromises);
if(this.prunecallback !== undefined && victimList.length > 0){
this.prunecallback(victimList);
}
const survivorList = lru.slice(victimCount);
return this.setLRU(survivorList);
}
public async getAll() {
const keys = await this.backend.getAllKeys();
const namespaceKeys = keys.filter((key: string) => {
return key.substr(0, this.namespace.length) === this.namespace;
});
const results = await this.backend.multiGet(namespaceKeys);
const allEntries: { [key: string]: any } = {};
for (const [compositeKey, value] of results) {
const key = this.fromCompositeKey(compositeKey);
if (key === "_lru") {
continue;
}
allEntries[key] = JSON.parse(value);
}
return allEntries;
}
public async get(key: string): Promise<string | undefined> {
const value = await this.peek(key);
if (!value) {
return;
}
this.refreshLRU(key);
return value;
}
public async peek(key: string): Promise<string | undefined> {
const compositeKey = this.makeCompositeKey(key);
const entryJsonString = await this.backend.getItem(compositeKey);
let entry;
if (entryJsonString) {
entry = JSON.parse(entryJsonString);
}
let value;
if (entry) {
value = entry.value;
if (this.policy.stdTTL > 0) {
const deadline = entry.created.getTime() + this.policy.stdTTL * 1000;
const now = Date.now();
if (deadline < now) {
this.remove(key);
value = undefined;
}
}
}
return value;
}
public async remove(key: string): Promise<void> {
const compositeKey = this.makeCompositeKey(key);
await this.backend.removeItem(compositeKey);
return this.removeFromLRU(key);
}
public async set(key: string, value: string): Promise<void> {
const entry = {
created: new Date(),
value
};
const compositeKey = this.makeCompositeKey(key);
const entryString = JSON.stringify(entry);
await this.backend.setItem(compositeKey, entryString);
await this.refreshLRU(key);
return this.enforceLimits();
}
protected async addToLRU(key: string) {
const lru = await this.getLRU();
lru.push(key);
return this.setLRU(lru);
}
protected async getLRU() {
const lruString = await this.backend.getItem(this.getLRUKey());
let lru: string[];
if (!lruString) {
lru = [];
} else {
lru = JSON.parse(lruString);
}
return lru;
}
protected getLRUKey() {
return this.makeCompositeKey("_lru");
}
protected makeCompositeKey(key: string) {
return `${this.namespace}:${key}`;
}
protected fromCompositeKey(compositeKey: string) {
return compositeKey.slice(this.namespace.length + 1);
}
protected async refreshLRU(key: string) {
await this.removeFromLRU(key);
return this.addToLRU(key);
}
protected async removeFromLRU(key: string) {
const lru = await this.getLRU();
const newLRU = lru.filter((item: string) => {
return item !== key;
});
return this.setLRU(newLRU);
}
protected async setLRU(lru: string[]) {
return this.backend.setItem(this.getLRUKey(), JSON.stringify(lru));
}
}