-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcache.js
112 lines (100 loc) · 2.71 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Copyright 2013 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var fs = require('fs');
/**
* @const
* @private
* Path to the temporary directory.
* @type {String}
*/
Cache.TMP_DIR_ = __dirname + '/../.cache';
/**
* @const
* @private
* Default encoding for cached files.
* @type {String}
*/
Cache.ENCODING_ = 'utf-8';
/**
* @const
* @private
* Default expires time in micro seconds.
* @type {Number}
*/
Cache.EXPIRES_IN_ = 5 * 60 * 1000;
/**
* @constructor
* Cache constructor.
* @param {String} opt_opts Optional options.
*/
function Cache(opt_opts) {
this.opts = opt_opts || {};
this.opts.path = this.opts.path || Cache.TMP_DIR_;
if (!fs.existsSync(this.opts.path)) {
fs.mkdirSync(this.opts.path, '700');
}
}
/**
* Loads discovery data from cache
* @param {object} api
* @return {object?} Returns the cached discovery data if exists
* or not expired.
*/
Cache.prototype.load = function(api) {
var path = this.getPath(api);
// check if file exists and not expired
if (!fs.existsSync(path)) {
return null;
}
if (this.isExpired(path)) {
// delete the expired cache file
fs.unlinkSync(path);
return null;
}
var data = fs.readFileSync(this.getPath(api), Cache.ENCODING_);
return data && JSON.parse(data);
};
/**
* Writes api discovery data to the cache.
* @param {object} api
* @param {object} data
*/
Cache.prototype.write = function(api, data) {
data && fs.writeFileSync(
this.getPath(api), JSON.stringify(data), Cache.ENCODING_);
};
/**
* Checks whether the file at path is expired or not
* @param {[type]} path The path of the file to check.
* @return {Boolean} Returns true or false depending on the expiration.
*/
Cache.prototype.isExpired = function(path) {
var expiresIn = this.opts.expiresIn || Cache.EXPIRES_IN_;
return new Date(fs.statSync(path).mtime).getTime() - expiresIn < 0;
};
/**
* Gets the path where the api data is stored at.
* @param {[type]} api
* @return {string} Path to the data file.
*/
Cache.prototype.getPath = function(api) {
return this.opts.path + '/' + api.name + api.version + '-rest';
};
/**
* Exports Cache.
* @type {Cache}
*/
module.exports = Cache;