forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcache.js
47 lines (37 loc) · 951 Bytes
/
cache.js
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
'use strict';
var path = require('path');
var file = require('./file');
const cache = {};
cache.init = function() {
file.mkdir(file.cacheDir());
};
cache.get = function(k) {
const fullpath = file.cacheFile(k);
if (!file.exist(fullpath)) return null;
return JSON.parse(file.data(fullpath));
};
cache.set = function(k, v) {
const fullpath = file.cacheFile(k);
file.write(fullpath, JSON.stringify(v));
return true;
};
cache.del = function(k) {
const fullpath = file.cacheFile(k);
if (!file.exist(fullpath)) return false;
file.rm(fullpath);
return true;
};
cache.list = function() {
return file.list(file.cacheDir())
.filter(x => path.extname(x) === '.json')
.map(function(filename) {
const k = path.basename(filename, '.json');
const stat = file.stat(file.cacheFile(k));
return {
name: k,
size: stat.size,
mtime: stat.mtime
};
});
};
module.exports = cache;