保誠-保戶業務員媒合平台
Tomas
2022-05-19 957a1f10a06fdbb76f1a0ba94fe44126c613fee3
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
const cacache = require('cacache');
 
class CacacheSerializer {
  constructor({ cacheDirPath }) {
    this.path = cacheDirPath;
  }
 
  read() {
    const cache = {};
    const promises = [];
    return new Promise((resolve, reject) => {
      cacache.ls
        .stream(this.path)
        .on('data', ({ key }) => {
          promises.push(
            cacache.get(this.path, key).then(({ data }) => {
              cache[key] = JSON.parse(data);
            }),
          );
        })
        .on('error', reject)
        .on('end', () => {
          resolve();
        });
    })
      .then(() => Promise.all(promises))
      .then(() => cache);
  }
 
  write(ops) {
    return Promise.all(
      ops.map(op => {
        if (op.value) {
          return cacache.put(this.path, op.key, JSON.stringify(op.value));
        } else {
          return cacache.rm.entry(this.path, op.key);
        }
      }),
    );
  }
}
 
module.exports = CacacheSerializer;