forked from BV-BRC/BV-BRC-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
64 lines (50 loc) · 1.19 KB
/
Copy pathcache.js
File metadata and controls
64 lines (50 loc) · 1.19 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var debug = require('debug')('p3api-server:cachemiddleware');
var fs = require('fs-extra');
var conf = require("./config");
var Path = require("path");
var Deferred = require("promised-io/promise").defer;
var touch = require("touch");
var cacheDir = conf.get("cache").directory;
debug("Using Cache Dir: ", cacheDir);
module.exports = {
get: function(key, options){
var def = new Deferred();
options = options || {};
if(!options.user){
options.user = 'public'
}
var fn = Path.join(cacheDir, options.user, key);
debug("Check for Cached Data in: ", fn);
fs.exists(fn, function(exists){
if(!exists){
def.reject(false);
return;
}
fs.readJson(fn, function(err, data){
if(err){
return def.reject(err);
}
def.resolve(data);
touch(fn);
});
});
return def.promise;
},
put: function(key, data, options){
options = options || {};
if(!options.user){
options.user = 'public'
}
var def = new Deferred();
var fn = Path.join(cacheDir, options.user, key);
debug("Store Cached Data to: ", fn);
fs.outputJson(fn, data, function(err){
if(err){
def.reject(err);
return;
}
def.resolve(true);
});
return def.promise;
}
};