1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const Cache = require("../Cache");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | class MemoryWithGcCachePlugin {
|
16 | constructor({ maxGenerations }) {
|
17 | this._maxGenerations = maxGenerations;
|
18 | }
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 | apply(compiler) {
|
25 | const maxGenerations = this._maxGenerations;
|
26 |
|
27 | const cache = new Map();
|
28 |
|
29 | const oldCache = new Map();
|
30 | let generation = 0;
|
31 | let cachePosition = 0;
|
32 | const logger = compiler.getInfrastructureLogger("MemoryWithGcCachePlugin");
|
33 | compiler.hooks.afterDone.tap("MemoryWithGcCachePlugin", () => {
|
34 | generation++;
|
35 | let clearedEntries = 0;
|
36 | let lastClearedIdentifier;
|
37 | for (const [identifier, entry] of oldCache) {
|
38 | if (entry.until > generation) break;
|
39 |
|
40 | oldCache.delete(identifier);
|
41 | if (cache.get(identifier) === undefined) {
|
42 | cache.delete(identifier);
|
43 | clearedEntries++;
|
44 | lastClearedIdentifier = identifier;
|
45 | }
|
46 | }
|
47 | if (clearedEntries > 0 || oldCache.size > 0) {
|
48 | logger.log(
|
49 | `${cache.size - oldCache.size} active entries, ${
|
50 | oldCache.size
|
51 | } recently unused cached entries${
|
52 | clearedEntries > 0
|
53 | ? `, ${clearedEntries} old unused cache entries removed e. g. ${lastClearedIdentifier}`
|
54 | : ""
|
55 | }`
|
56 | );
|
57 | }
|
58 | let i = (cache.size / maxGenerations) | 0;
|
59 | let j = cachePosition >= cache.size ? 0 : cachePosition;
|
60 | cachePosition = j + i;
|
61 | for (const [identifier, entry] of cache) {
|
62 | if (j !== 0) {
|
63 | j--;
|
64 | continue;
|
65 | }
|
66 | if (entry !== undefined) {
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | cache.set(identifier, undefined);
|
72 | oldCache.delete(identifier);
|
73 | oldCache.set(identifier, {
|
74 | entry,
|
75 | until: generation + maxGenerations
|
76 | });
|
77 | if (i-- === 0) break;
|
78 | }
|
79 | }
|
80 | });
|
81 | compiler.cache.hooks.store.tap(
|
82 | { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
|
83 | (identifier, etag, data) => {
|
84 | cache.set(identifier, { etag, data });
|
85 | }
|
86 | );
|
87 | compiler.cache.hooks.get.tap(
|
88 | { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
|
89 | (identifier, etag, gotHandlers) => {
|
90 | const cacheEntry = cache.get(identifier);
|
91 | if (cacheEntry === null) {
|
92 | return null;
|
93 | } else if (cacheEntry !== undefined) {
|
94 | return cacheEntry.etag === etag ? cacheEntry.data : null;
|
95 | }
|
96 | const oldCacheEntry = oldCache.get(identifier);
|
97 | if (oldCacheEntry !== undefined) {
|
98 | const cacheEntry = oldCacheEntry.entry;
|
99 | if (cacheEntry === null) {
|
100 | oldCache.delete(identifier);
|
101 | cache.set(identifier, cacheEntry);
|
102 | return null;
|
103 | } else {
|
104 | if (cacheEntry.etag !== etag) return null;
|
105 | oldCache.delete(identifier);
|
106 | cache.set(identifier, cacheEntry);
|
107 | return cacheEntry.data;
|
108 | }
|
109 | }
|
110 | gotHandlers.push((result, callback) => {
|
111 | if (result === undefined) {
|
112 | cache.set(identifier, null);
|
113 | } else {
|
114 | cache.set(identifier, { etag, data: result });
|
115 | }
|
116 | return callback();
|
117 | });
|
118 | }
|
119 | );
|
120 | compiler.cache.hooks.shutdown.tap(
|
121 | { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
|
122 | () => {
|
123 | cache.clear();
|
124 | oldCache.clear();
|
125 | }
|
126 | );
|
127 | }
|
128 | }
|
129 | module.exports = MemoryWithGcCachePlugin;
|