-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcache.ts
302 lines (281 loc) · 8.33 KB
/
cache.ts
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/** Utilities which cache information from the datastore in memory to help
* increase performance.
*
* @module
*/
import { type Datastore, entityToObject } from "google_datastore";
import type { Key } from "google_datastore/types";
import { getDatastore } from "./auth.ts";
import { kinds } from "./consts.ts";
import { InfoPage, Module, ModuleEntry, ModuleVersion } from "./types.d.ts";
import { assert } from "./util.ts";
const CACHED_MODULE_COUNT =
parseInt(Deno.env.get("CACHED_MODULE_COUNT") ?? "", 10) ||
100;
const cachedModules = new Map<string, Module>();
const cachedVersions = new WeakMap<Module, Map<string, ModuleVersion>>();
const cachedEntries = new WeakMap<ModuleVersion, Map<string, ModuleEntry>>();
const cachedInfoPages = new WeakMap<Module, Map<string, InfoPage>>();
/** The "LRU" for modules names. */
const cachedModuleNames = new Set<string>();
let bc: BroadcastChannel | undefined;
if ("BroadcastChannel" in globalThis) {
console.log(
"%cStarting%c cache broadcast channel.",
"color:green",
"color:none",
);
bc = new BroadcastChannel("cache_clear");
bc.addEventListener("message", ({ data }) => {
const module = String(data);
console.log(
`%cReceived%c clear cache for module: %c"${module}"%c.`,
"color:green",
"color:none",
"color:cyan",
"color:none",
);
cachedModules.delete(module);
cachedModuleNames.delete(module);
});
}
/** Lazily set datastore. */
let datastore: Datastore | undefined;
let pruneQueued = false;
function prune() {
const evictionCount = cachedModuleNames.size - CACHED_MODULE_COUNT;
if (evictionCount > 0) {
const toEvict: string[] = [];
for (const moduleName of cachedModuleNames) {
toEvict.push(moduleName);
if (toEvict.length >= evictionCount) {
break;
}
}
for (const moduleName of toEvict) {
cachedModuleNames.delete(moduleName);
cachedModules.delete(moduleName);
console.log(
`%cEvicting%c module %c"${moduleName}"%c from cache.`,
"color:green",
"color:none",
"color:cyan",
"color:none",
);
}
}
pruneQueued = false;
}
function enqueuePrune() {
if (!pruneQueued) {
pruneQueued = true;
queueMicrotask(prune);
}
}
export function clear(module?: string) {
if (module) {
cachedModules.delete(module);
cachedModuleNames.delete(module);
bc?.postMessage(module);
} else {
cachedModules.clear();
cachedModuleNames.clear();
}
}
export async function lookupInfoPage(
module: string,
version: string,
): Promise<InfoPage | undefined> {
let moduleItem = cachedModules.get(module);
let infoPageItem = moduleItem &&
cachedInfoPages.get(moduleItem)?.get(version);
if (!infoPageItem) {
datastore = datastore || await getDatastore();
const keys: Key[] = [];
if (!moduleItem) {
keys.push(datastore.key([kinds.MODULE_KIND, module]));
}
keys.push(datastore.key(
[kinds.MODULE_KIND, module],
[kinds.INFO_PAGE_KIND, version],
));
const res = await datastore.lookup(keys);
if (res.found) {
for (const { entity } of res.found) {
assert(entity.key);
const entityKind = entity.key.path[entity.key.path.length - 1].kind;
switch (entityKind) {
case kinds.MODULE_KIND:
moduleItem = entityToObject(entity);
cachedModules.set(module, moduleItem);
break;
case kinds.INFO_PAGE_KIND: {
infoPageItem = entityToObject(entity);
assert(moduleItem);
if (!cachedInfoPages.has(moduleItem)) {
cachedInfoPages.set(moduleItem, new Map());
}
const infoPages = cachedInfoPages.get(moduleItem)!;
infoPages.set(version, infoPageItem);
break;
}
}
}
}
}
if (moduleItem) {
cachedModuleNames.add(module);
enqueuePrune();
}
return infoPageItem;
}
export async function lookup(
module: string,
): Promise<[Module | undefined, undefined, undefined]>;
export async function lookup(
module: string,
version: string,
): Promise<[Module | undefined, ModuleVersion | undefined, undefined]>;
export async function lookup(
module: string,
version: string,
path: string,
): Promise<
[Module | undefined, ModuleVersion | undefined, ModuleEntry | undefined]
>;
export async function lookup(
module: string,
version?: string,
path?: string,
) {
let moduleItem = cachedModules.get(module);
let versionItem = version && moduleItem &&
cachedVersions.get(moduleItem)?.get(version) || undefined;
let entryItem = path && versionItem &&
cachedEntries.get(versionItem)?.get(path) || undefined;
datastore = datastore || await getDatastore();
const keys: Key[] = [];
if (!moduleItem) {
keys.push(datastore.key(
[kinds.MODULE_KIND, module],
));
}
if (version && !versionItem) {
keys.push(datastore.key(
[kinds.MODULE_KIND, module],
[kinds.MODULE_VERSION_KIND, version],
));
}
if (path && !entryItem) {
assert(version);
keys.push(datastore.key(
[kinds.MODULE_KIND, module],
[kinds.MODULE_VERSION_KIND, version],
[kinds.MODULE_ENTRY_KIND, path],
));
}
if (keys.length) {
const res = await datastore.lookup(keys);
if (res.found) {
for (const { entity } of res.found) {
assert(entity.key);
const entityKind = entity.key.path[entity.key.path.length - 1].kind;
switch (entityKind) {
case kinds.MODULE_KIND:
moduleItem = entityToObject(entity);
cachedModules.set(module, moduleItem);
break;
case kinds.MODULE_VERSION_KIND: {
versionItem = entityToObject(entity);
queueMicrotask(() => {
assert(versionItem);
assert(moduleItem);
assert(version);
if (!cachedVersions.has(moduleItem)) {
cachedVersions.set(moduleItem, new Map());
}
const versions = cachedVersions.get(moduleItem)!;
versions.set(version, versionItem);
});
break;
}
case kinds.MODULE_ENTRY_KIND: {
entryItem = entityToObject(entity);
queueMicrotask(() => {
assert(entryItem);
assert(versionItem);
assert(path);
if (!cachedEntries.has(versionItem)) {
cachedEntries.set(versionItem, new Map());
}
const entries = cachedEntries.get(versionItem)!;
entries.set(path, entryItem);
});
break;
}
default:
throw new TypeError(`Unexpected kind "${entityKind}"`);
}
}
}
}
if (moduleItem) {
cachedModuleNames.add(module);
enqueuePrune();
}
return [moduleItem, versionItem, entryItem];
}
export function cacheModule(module: string, moduleItem: Module): void {
cachedModules.set(module, moduleItem);
cachedModuleNames.add(module);
}
export function cacheModuleVersion(
module: string,
version: string,
versionItem: ModuleVersion,
): void {
const moduleItem = cachedModules.get(module);
if (moduleItem) {
if (!cachedVersions.has(moduleItem)) {
cachedVersions.set(moduleItem, new Map());
}
const versions = cachedVersions.get(moduleItem)!;
versions.set(version, versionItem);
cachedModuleNames.add(module);
}
}
export function cacheModuleEntry(
module: string,
version: string,
path: string,
entry: ModuleEntry,
): void {
const moduleItem = cachedModules.get(module);
if (moduleItem) {
const versionItem = cachedVersions.get(moduleItem)?.get(version);
if (versionItem) {
if (!cachedEntries.has(versionItem)) {
cachedEntries.set(versionItem, new Map());
}
const entries = cachedEntries.get(versionItem)!;
entries.set(path, entry);
cachedModuleNames.add(module);
}
}
}
export function cacheInfoPage(
module: string,
version: string,
infoPage: InfoPage,
): void {
const moduleItem = cachedModules.get(module);
if (moduleItem) {
if (!cachedInfoPages.has(moduleItem)) {
cachedInfoPages.set(moduleItem, new Map());
}
const infoPages = cachedInfoPages.get(moduleItem)!;
infoPages.set(version, infoPage);
cachedModuleNames.add(module);
}
}