sync-disk-cache
Advanced tools
Comparing version 1.3.4 to 2.0.0
82
index.js
'use strict'; | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var readFile = fs.readFileSync; | ||
var writeFile = fs.writeFileSync; | ||
var renameFile = fs.renameSync; | ||
var mkdirp = require('mkdirp').sync; | ||
var rimraf = require('rimraf').sync; | ||
var unlink = fs.unlinkSync; | ||
var chmod = fs.chmodSync; | ||
var debug = require('debug')('sync-disk-cache'); | ||
var zlib = require('zlib'); | ||
var heimdall = require('heimdalljs'); | ||
var os = require('os'); | ||
var username = require('username-sync')(); | ||
var tmpdir = path.join(os.tmpdir(), username); | ||
var crypto = require('crypto'); | ||
var mode = { | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const readFile = fs.readFileSync; | ||
const writeFile = fs.writeFileSync; | ||
const renameFile = fs.renameSync; | ||
const mkdirp = require('mkdirp').sync; | ||
const rimraf = require('rimraf').sync; | ||
const unlink = fs.unlinkSync; | ||
const chmod = fs.chmodSync; | ||
const debug = require('debug')('sync-disk-cache'); | ||
const zlib = require('zlib'); | ||
const heimdall = require('heimdalljs'); | ||
const os = require('os'); | ||
const username = require('username-sync')(); | ||
const tmpdir = path.join(os.tmpdir(), username); | ||
const crypto = require('crypto'); | ||
const mode = { | ||
mode: parseInt('0777', 8) | ||
}; | ||
var CacheEntry = require('./lib/cache-entry'); | ||
var Metric = require('./lib/metric'); | ||
const CacheEntry = require('./lib/cache-entry'); | ||
const Metric = require('./lib/metric'); | ||
@@ -44,8 +44,8 @@ if (!heimdall.hasMonitor('sync-disk-cache')) { | ||
obj[name] = function() { | ||
var stats = heimdall.statsFor('sync-disk-cache'); | ||
var metrics = stats[name] = stats[name] || new Metric(); | ||
let stats = heimdall.statsFor('sync-disk-cache'); | ||
let metrics = stats[name] = stats[name] || new Metric(); | ||
metrics.start(); | ||
var result; | ||
let result; | ||
try { | ||
@@ -92,3 +92,3 @@ result = fn.apply(this, arguments); | ||
var COMPRESSIONS = { | ||
const COMPRESSIONS = { | ||
deflate: { | ||
@@ -124,16 +124,18 @@ in: zlib.deflateSync, | ||
*/ | ||
function Cache(key, _) { | ||
var options = _ || {}; | ||
this.tmpdir = options.location|| tmpdir; | ||
class Cache{ | ||
constructor(key, _) { | ||
const options = _ || {}; | ||
this.tmpdir = options.location|| tmpdir; | ||
if (options.compression) { | ||
hasCompression(options.compression) | ||
} | ||
this.compression = options.compression; | ||
if (options.compression) { | ||
hasCompression(options.compression) | ||
} | ||
this.compression = options.compression; | ||
this.key = key || 'default-disk-cache'; | ||
this.root = path.join(this.tmpdir, 'if-you-need-to-delete-this-open-an-issue-sync-disk-cache', this.key); | ||
this.key = key || 'default-disk-cache'; | ||
this.root = path.join(this.tmpdir, 'if-you-need-to-delete-this-open-an-issue-sync-disk-cache', this.key); | ||
debug('new Cache { root: %s, compression: %s }', this.root, this.compression); | ||
debug('new Cache { root: %s, compression: %s }', this.root, this.compression); | ||
} | ||
} | ||
@@ -162,3 +164,3 @@ | ||
defineFunction(Cache.prototype, 'has', function(key) { | ||
var filePath = this.pathFor(key); | ||
const filePath = this.pathFor(key); | ||
debug('has: %s', filePath); | ||
@@ -177,3 +179,3 @@ | ||
defineFunction(Cache.prototype, 'get', function(key) { | ||
var filePath = this.pathFor(key); | ||
const filePath = this.pathFor(key); | ||
debug('get: %s', filePath); | ||
@@ -188,3 +190,3 @@ | ||
var MAX_DIGITS = Math.pow(10, (Number.MAX_SAFE_INTEGER + '').length); | ||
const MAX_DIGITS = Math.pow(10, (Number.MAX_SAFE_INTEGER + '').length); | ||
@@ -200,6 +202,6 @@ /* | ||
defineFunction(Cache.prototype, 'set', function(key, value) { | ||
var filePath = this.pathFor(key); | ||
const filePath = this.pathFor(key); | ||
debug('set : %s', filePath); | ||
var random = Math.random() * MAX_DIGITS; | ||
var tmpfile = filePath + '.tmp.' + random; | ||
const random = Math.random() * MAX_DIGITS; | ||
const tmpfile = filePath + '.tmp.' + random; | ||
@@ -230,3 +232,3 @@ try { | ||
defineFunction(Cache.prototype, 'remove', function(key) { | ||
var filePath = this.pathFor(key); | ||
const filePath = this.pathFor(key); | ||
debug('remove : %s', filePath); | ||
@@ -233,0 +235,0 @@ |
'use strict'; | ||
function CacheEntry(isCached, key, value) { | ||
this.isCached = isCached; | ||
this.key = key; | ||
this.value = value; | ||
} | ||
module.exports = class CacheEntry { | ||
constructor(isCached, key, value) { | ||
this.isCached = isCached; | ||
this.key = key; | ||
this.value = value; | ||
} | ||
}; | ||
module.exports = CacheEntry; | ||
module.exports.MISS = new CacheEntry(false); | ||
module.exports.MISS = new module.exports(false); | ||
@@ -1,26 +0,28 @@ | ||
function Metric() { | ||
this.count = 0; | ||
this.time = 0; | ||
this.startTime = undefined; | ||
} | ||
'use strict'; | ||
Metric.prototype.start = function() { | ||
this.startTime = process.hrtime(); | ||
this.count++; | ||
}; | ||
module.exports = class Metric { | ||
constructor() { | ||
this.count = 0; | ||
this.time = 0; | ||
this.startTime = undefined; | ||
} | ||
Metric.prototype.stop = function() { | ||
var change = process.hrtime(this.startTime); | ||
start() { | ||
this.startTime = process.hrtime(); | ||
this.count++; | ||
} | ||
this.time += change[0] * 1e9 + change[1]; | ||
this.startTime = undefined; | ||
}; | ||
stop() { | ||
let change = process.hrtime(this.startTime); | ||
Metric.prototype.toJSON = function() { | ||
return { | ||
count: this.count, | ||
time: this.time | ||
}; | ||
}; | ||
this.time += change[0] * 1e9 + change[1]; | ||
this.startTime = undefined; | ||
} | ||
module.exports = Metric; | ||
toJSON() { | ||
return { | ||
count: this.count, | ||
time: this.time | ||
}; | ||
} | ||
} |
{ | ||
"name": "sync-disk-cache", | ||
"version": "1.3.4", | ||
"version": "2.0.0", | ||
"description": "sync disk cache", | ||
@@ -29,13 +29,16 @@ "files": [ | ||
"dependencies": { | ||
"debug": "^2.1.3", | ||
"heimdalljs": "^0.2.3", | ||
"debug": "^4.1.1", | ||
"heimdalljs": "^0.2.6", | ||
"mkdirp": "^0.5.0", | ||
"rimraf": "^2.2.8", | ||
"rimraf": "^3.0.0", | ||
"username-sync": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "^0.3.6", | ||
"mocha": "^2.1.0", | ||
"should": "^5.0.1" | ||
"istanbul": "^0.4.5", | ||
"mocha": "^6.2.2", | ||
"should": "^13.2.3" | ||
}, | ||
"engines": { | ||
"node": "8.* || >= 10.*" | ||
}, | ||
"jscsConfig": { | ||
@@ -42,0 +45,0 @@ "preset": "google", |
10646
269
+ Addeddebug@4.3.5(transitive)
+ Addedms@2.1.2(transitive)
+ Addedrimraf@3.0.2(transitive)
- Removeddebug@2.6.9(transitive)
- Removedms@2.0.0(transitive)
- Removedrimraf@2.7.1(transitive)
Updateddebug@^4.1.1
Updatedheimdalljs@^0.2.6
Updatedrimraf@^3.0.0