-
Notifications
You must be signed in to change notification settings - Fork 12
/
cache.js
57 lines (49 loc) · 1.33 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
51
52
53
54
55
56
57
const debug = require('debug')('p3api-server:cacheClass')
const Fs = require('fs-extra')
const Config = require('./config')
const Path = require('path')
const Touch = require('touch')
const CACHE_DIR = Config.get('cache').directory
debug('Using Cache Dir: ', CACHE_DIR)
module.exports = {
get: function (key, options) {
options = options || {}
if (!options.user) {
options.user = 'public'
}
return new Promise((resolve, reject) => {
const fileName = Path.join(CACHE_DIR, options.user, key)
debug('Check for Cached Data in: ', fileName)
Fs.exists(fileName, (exists) => {
if (!exists) {
reject(new Error(`File does not exist`))
return
}
Fs.readJson(fileName, (err, data) => {
if (err) {
return reject(err)
}
resolve(data)
Touch(fileName)
})
})
})
},
put: function (key, data, options) {
options = options || {}
if (!options.user) {
options.user = 'public'
}
return new Promise((resolve, reject) => {
const fileName = Path.join(CACHE_DIR, options.user, key)
debug('Store Cached Data to: ', fileName)
Fs.outputJson(fileName, data, (err) => {
if (err) {
reject(err)
return
}
resolve(true)
})
})
}
}