forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcache.js
50 lines (40 loc) · 1.01 KB
/
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
48
49
50
var fs = require('fs');
var path = require('path');
var h = require('./helper');
var cache = {};
cache.init = function() {
h.mkdir(h.getCacheDir());
};
cache.get = function(k) {
var fullpath = h.getCacheFile(k);
if (!fs.existsSync(fullpath)) return null;
var v = JSON.parse(fs.readFileSync(fullpath));
return v;
};
cache.set = function(k, v) {
var fullpath = h.getCacheFile(k);
fs.writeFileSync(fullpath, JSON.stringify(v));
return true;
};
cache.del = function(k) {
var fullpath = h.getCacheFile(k);
if (!fs.existsSync(fullpath)) return false;
fs.unlinkSync(fullpath);
return true;
};
cache.list = function() {
return fs.readdirSync(h.getCacheDir())
.filter(function(filename) {
return path.extname(filename) === '.json';
})
.map(function(filename) {
var k = path.basename(filename, '.json');
var stat = fs.statSync(h.getCacheFile(k));
return {
name: k,
size: stat.size,
mtime: stat.mtime
};
});
};
module.exports = cache;