forked from kuja/hamster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.js
More file actions
223 lines (189 loc) · 4.81 KB
/
Copy pathfile.js
File metadata and controls
223 lines (189 loc) · 4.81 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var fs = require('fs')
, path = require('path')
, crypto = require('crypto')
, Cache = require('./cache')
module.exports = FileCache
/**
* File cache.
*
* The following options are recognized:
* <ul>
* <li><strong>path</strong>: Path to directory where cache files will be saved</li>
* <li><strong>prefix</strong>: Prefix to be used in cache file names (default: <tt>''</tt>)</li>
* </ul>
*
* @exports FileCache as hamster.cache.FileCache
* @param {Object} options
* @constructor
*/
function FileCache(options) {
Cache.call(this)
var tmpDir = process.env.TMPDIR || process.env.TEMP || '/tmp'
options = options || {}
this.setPath(options.path || path.join(tmpDir, 'hamster-cache'))
this.setPrefix(options.prefix || '')
}
FileCache.prototype = Object.create(Cache.prototype)
/**
* Set path to cache directory.
*
* @param {String} p Directory path
*/
FileCache.prototype.setPath = function (p) {
this._path = p
}
/**
* Get path to cache directory.
*
* @return {String} Directory path
*/
FileCache.prototype.getPath = function () {
return this._path
}
/**
* Set file name prefix.
*
* @param {String} prefix Prefix string
*/
FileCache.prototype.setPrefix = function (prefix) {
this._prefix = prefix
}
/**
* Get file name prefix.
*
* @return {String} Prefix string
*/
FileCache.prototype.getPrefix = function () {
return this._prefix
}
/**
* Get path to file identified by the specified cache key.
*
* @param {String} key Cache key
* @return {String} Path
*/
FileCache.prototype.getFilePath = function (key) {
var hash = crypto.createHash('sha1')
, sha1 = hash.update(key).digest('hex')
, file = this.getPrefix() + sha1
return path.join(this.getPath(), sha1.substr(0, 2), sha1.substr(2, 2), file)
}
/**
* Clear file cache.
*
* @param {Function} cb Callback
*/
FileCache.prototype.clear = function (cb) {
if (!cb) cb = function () {}
var self = this
var clearDir = function (dir, cb) {
fs.readdir(dir, function (err, files) {
if (err) throw err
if (!files.length) return cb(null)
var remaining = files.length
var removeDir = function () {
if (!(--remaining)) {
if (self.getPath() === dir) return cb(null)
fs.rmdir(dir, function () {
cb(null)
})
}
}
files.forEach(function (file) {
file = path.join(dir, file)
fs.stat(file, function (err, stats) {
if (err) throw err
if (stats.isDirectory()) {
clearDir(file, function (err) {
if (err) throw err
fs.rmdir(file, removeDir)
})
} else {
fs.unlink(file, removeDir)
}
})
})
})
}
try {
clearDir(this.getPath(), cb)
} catch (err) {
cb(err)
}
}
/**
* Recursively create directories.
*
* @param {String} dir Dir name
* @param {Function} cb Callback
*/
FileCache.prototype.makeDirs = function (dir, cb) {
if (!cb) cb = function () {}
var self = this
fs.exists(dir, function (exists) {
if (exists) {
cb(null)
} else {
fs.mkdir(dir, function (err) {
if (err) {
if (err.code !== 'ENOENT') return cb(err)
self.makeDirs(path.dirname(dir), function (err) {
if (err) return cb(err)
self.makeDirs(dir, cb)
})
} else {
cb(null)
}
})
}
})
}
/**
* Store value in cache.
*
* @param {String} key Cache key
* @param {String} value Cache Value
* @param {Number} duration Number of seconds this cache entry will live
* @param {Function} cb Callback
*/
FileCache.prototype.write = function (key, value, duration, cb) {
var file = this.getFilePath(key)
, dir = path.dirname(file)
, self = this
self.makeDirs(dir, function (err) {
if (err) return cb(err)
fs.open(file, 'w', function (err, fd) {
if (err) return cb(err)
var meta = JSON.stringify({expireTime: (new Date()).getTime() + duration})
, data = new Buffer([meta, value].join('\n'))
fs.write(fd, data, 0, data.length, 0, function (err, written, buffer) {
if (err) return cb(err)
fs.close(fd, function () { cb(null) })
})
})
})
}
/**
* Retrieve value from cache.
*
* @param {String} key Cache key
* @param {Function} cb Callback
* @return {String} Cache value
*/
FileCache.prototype.read = function (key, cb) {
var file = this.getFilePath(key)
, self = this
fs.readFile(file, function (err, data) {
if (err) {
if (err.code === 'ENOENT') return cb(null)
return cb(err)
}
data = data.toString().split('\n', 2)
var meta = JSON.parse(data[0])
if (self.getCurrentTime() >= meta.expireTime) {
fs.unlink(file, function (err) { cb(err) })
} else {
cb(null, data[1])
}
})
}