memo-again
Advanced tools
Comparing version
{ | ||
"name": "memo-again", | ||
"version": "0.3.12", | ||
"version": "0.4.0", | ||
"description": "Serializable memoizer with promises", | ||
@@ -30,3 +30,6 @@ "main": "index.js", | ||
"keywords": [ | ||
"memoizer", "nodejs", "file cache", "memory cache" | ||
"memoizer", | ||
"nodejs", | ||
"file cache", | ||
"memory cache" | ||
], | ||
@@ -33,0 +36,0 @@ "author": "Karl Lew", |
@@ -29,10 +29,9 @@ (function(exports) { | ||
this.pruning = 0; | ||
this.size = { | ||
total: 0, | ||
pruned: 0, | ||
}; | ||
this.bytesScanned = 0; | ||
this.bytesPruned = 0; | ||
this.filesPruned = 0; | ||
} | ||
static onPrune(oldPath, stats) { | ||
logger.debug("prune", oldPath); | ||
logger.debug("FilePruner.onPrune()", oldPath); | ||
return true; | ||
@@ -51,14 +50,17 @@ } | ||
var pruneDays = this.pruneDays || 180; | ||
var pruned = []; | ||
this.started = new Date(); | ||
this.size.total = 0; | ||
this.size.pruned = 0; | ||
this.bytesScanned = 0; | ||
this.bytesPruned = 0; | ||
this.earliest = Date.now(); | ||
var pruneDate = new Date(Date.now()-pruneDays*MS_DAY); | ||
this.log(`pruneOldFiles() started:${this.started}`); | ||
this.info(`pruneOldFiles() started:${this.started}`); | ||
var pruneOpts = { root, stats:true, absolute:true }; | ||
this.pruning = 1; | ||
let filesPruned = 0; | ||
let bytesPruned = 0; | ||
let bytesScanned = 0; | ||
for await (let f of Files.files(pruneOpts)) { | ||
var { stats, path:fpath } = f; | ||
this.size.total += stats.size; | ||
bytesScanned += stats.size; | ||
this.bytesScanned += stats.size; | ||
stats.mtime < this.earliest && | ||
@@ -68,6 +70,8 @@ (this.earliest = stats.mtime); | ||
if (await onPrune(fpath, stats)) { // qualified delete | ||
pruned.push(fpath); | ||
filesPruned++; | ||
this.filesPruned++; | ||
this.debug(`pruneOldFiles() unlink:${fpath}`); | ||
await fs.promises.unlink(fpath); | ||
this.size.pruned += stats.size; | ||
bytesPruned += stats.size; | ||
this.bytesPruned += stats.size; | ||
} | ||
@@ -79,3 +83,3 @@ } | ||
var elapsed = ((this.done - this.started)/1000).toFixed(1); | ||
this.log(`pruneOldFiles() done:${elapsed}s`); | ||
this.info(`pruneOldFiles() done:${elapsed}s`); | ||
return { | ||
@@ -85,5 +89,6 @@ started: this.started, | ||
done: this.done, | ||
size: this.size, | ||
bytesScanned, | ||
bytesPruned, | ||
filesPruned, | ||
pruning: this.pruning, | ||
pruned, | ||
} | ||
@@ -90,0 +95,0 @@ } catch(e) { |
@@ -12,6 +12,6 @@ (typeof describe === 'function') && describe("file-pruner", function() { | ||
var TEST_SOUNDS2 = path.join(__dirname, 'data', 'sounds2'); | ||
logger.level = 'warn'; | ||
logger.level = 'info'; | ||
this.timeout(5*1000); | ||
it("default ctor", ()=>{ | ||
it("TESTTESTdefault ctor", ()=>{ | ||
should.throws(()=>{ // root is required | ||
@@ -28,2 +28,5 @@ var fp = new FilePruner(); | ||
should(fp.done).equal(undefined); | ||
should(fp.bytesScanned).equal(0); | ||
should(fp.bytesPruned).equal(0); | ||
should(fp.filesPruned).equal(0); | ||
}); | ||
@@ -41,3 +44,3 @@ it("custom ctor", ()=>{ | ||
}); | ||
it("pruneOldFiles() handles errors ", async()=>{ | ||
it("TESTTESTpruneOldFiles() handles errors ", async()=>{ | ||
var root = TEST_SOUNDS2; | ||
@@ -61,3 +64,3 @@ var fp = new FilePruner({ root, }); | ||
}); | ||
it("pruneOldFiles() ", async()=>{ try { | ||
it("TESTTESTpruneOldFiles() ", async()=>{ try { | ||
var root = TEST_SOUNDS; | ||
@@ -86,4 +89,5 @@ var fp = new FilePruner({ root, }); | ||
var { | ||
pruned, | ||
size, | ||
filesPruned, | ||
bytesPruned, | ||
bytesScanned, | ||
done, | ||
@@ -93,11 +97,9 @@ started, | ||
} = await promise; | ||
should(pruned.length).equal(2); | ||
should(pruned[0]).match(/dummy3/); | ||
should(pruned[1]).match(/dummy1/); | ||
should(size).properties({ | ||
total: 174138, | ||
pruned: 21, | ||
}); | ||
should(filesPruned).equal(2); | ||
should(bytesScanned).equal(174138); | ||
should(bytesPruned).equal(21); | ||
should(fp).properties({ | ||
size, | ||
bytesScanned: 174138, | ||
bytesPruned: 21, | ||
filesPruned: 2, | ||
pruning: 0, | ||
@@ -115,3 +117,3 @@ }); | ||
}}); | ||
it("pruneOldFiles() custom onPrune", async()=>{ try { | ||
it("TESTTESTpruneOldFiles() custom onPrune", async()=>{ try { | ||
var root = TEST_SOUNDS; | ||
@@ -158,5 +160,6 @@ var aug262020 = new Date(2020,7,26); | ||
should(res.done - res.started).above(2*MSTEST).below(5000); | ||
should(res.size).properties({ | ||
total: 174138, | ||
pruned: 0, | ||
should(res).properties({ | ||
bytesScanned: 174138, | ||
bytesPruned: 0, | ||
filesPruned: 0, | ||
}); | ||
@@ -167,3 +170,7 @@ should(prunable).equal(21); // dummy1+dummy3 file sizes | ||
// nothing pruned | ||
should.deepEqual(res.pruned, []); | ||
should(res).properties({ | ||
bytesScanned: 174138, | ||
bytesPruned: 0, | ||
filesPruned: 0, | ||
}); | ||
should(fs.existsSync(dummy1)).equal(true); | ||
@@ -170,0 +177,0 @@ should(fs.existsSync(dummy2)).equal(true); |
240976
0.24%1305
0.93%