1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const Cache = require("../Cache");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | class MemoryCachePlugin {
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 | apply(compiler) {
|
22 |
|
23 | const cache = new Map();
|
24 | compiler.cache.hooks.store.tap(
|
25 | { name: "MemoryCachePlugin", stage: Cache.STAGE_MEMORY },
|
26 | (identifier, etag, data) => {
|
27 | cache.set(identifier, { etag, data });
|
28 | }
|
29 | );
|
30 | compiler.cache.hooks.get.tap(
|
31 | { name: "MemoryCachePlugin", stage: Cache.STAGE_MEMORY },
|
32 | (identifier, etag, gotHandlers) => {
|
33 | const cacheEntry = cache.get(identifier);
|
34 | if (cacheEntry === null) {
|
35 | return null;
|
36 | } else if (cacheEntry !== undefined) {
|
37 | return cacheEntry.etag === etag ? cacheEntry.data : null;
|
38 | }
|
39 | gotHandlers.push((result, callback) => {
|
40 | if (result === undefined) {
|
41 | cache.set(identifier, null);
|
42 | } else {
|
43 | cache.set(identifier, { etag, data: result });
|
44 | }
|
45 | return callback();
|
46 | });
|
47 | }
|
48 | );
|
49 | compiler.cache.hooks.shutdown.tap(
|
50 | { name: "MemoryCachePlugin", stage: Cache.STAGE_MEMORY },
|
51 | () => {
|
52 | cache.clear();
|
53 | }
|
54 | );
|
55 | }
|
56 | }
|
57 | module.exports = MemoryCachePlugin;
|