node-persist
Advanced tools
Comparing version 2.0.3 to 2.0.4
{ | ||
"name": "node-persist", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Super-easy (and fast) persistent data structures in Node.js, modeled after HTML5 localStorage", | ||
@@ -5,0 +5,0 @@ "main": "./src/node-persist.js", |
@@ -21,2 +21,3 @@ /* | ||
interval: false, | ||
expiredInterval: 2 * 60 * 1000, /* every 2 minutes */ | ||
ttl: false | ||
@@ -116,5 +117,7 @@ }, | ||
if (options.interval && options.interval > 0) { | ||
this._persistInterval = setInterval(this.persist.bind(this), options.interval); | ||
this.startPersistInterval(this.persist.bind(this)); | ||
} | ||
this.startExpiredKeysInterval(); | ||
Q.all(deferreds).then( | ||
@@ -146,2 +149,3 @@ function() { | ||
this.parseStorageDirSync(); | ||
this.startExpiredKeysInterval(); | ||
@@ -330,2 +334,27 @@ //start synchronous persisting, | ||
removeExpiredItems: function (callback) { | ||
callback = isFunction(callback) ? callback : noop; | ||
var deferred = Q.defer(); | ||
var deferreds = []; | ||
var keys = this.keys(); | ||
for (var i = 0; i < keys.length; i++) { | ||
if (this.isExpired(keys[i])) { | ||
deferreds.push(this.removeItem(keys[i])); | ||
} | ||
} | ||
Q.all(deferreds).then( | ||
function() { | ||
deferred.resolve(); | ||
callback(); | ||
}, | ||
function(err) { | ||
deferred.reject(err); | ||
callback(err); | ||
}); | ||
return deferred.promise; | ||
}, | ||
clear: function (callback) { | ||
@@ -629,6 +658,20 @@ callback = isFunction(callback) ? callback : noop; | ||
stopInterval: function () { | ||
startPersistInterval: function (persistFunction) { | ||
this.stopPersistInterval(); | ||
this._persistInterval = setInterval(persistFunction || this.persist.bind(this), this.options.interval); | ||
}, | ||
stopPersistInterval: function () { | ||
clearInterval(this._persistInterval); | ||
}, | ||
startExpiredKeysInterval: function () { | ||
this.stopExpiredKeysInterval(); | ||
this._expiredKeysInterval = setInterval(this.removeExpiredItems.bind(this), this.options.expiredInterval); | ||
}, | ||
stopExpiredKeysInterval: function () { | ||
clearInterval(this._expiredKeysInterval); | ||
}, | ||
log: function () { | ||
@@ -635,0 +678,0 @@ this.options && this.options.logging && console.log.apply(console, arguments); |
@@ -306,3 +306,3 @@ | ||
it("should respect an expired different ttl per setItem and delete the items", function(done) { | ||
this.timeout(10000); | ||
var storage = nodePersist.create(); | ||
@@ -312,3 +312,3 @@ | ||
dir: randDir(), | ||
ttl: 1000 // 1 second | ||
ttl: 1000 // 1 second, | ||
}); | ||
@@ -318,16 +318,36 @@ | ||
// wait 2 seconds, then try to read the file, should still be there because we asked this one to live for 5 seconds, despite the default 1 second ttl | ||
// wait 2 seconds, then try to read the item1 file, should still be there because we asked this one to live for 5 seconds, despite the default 1 second ttl | ||
setTimeout(function() { | ||
var value = storage.getItemSync("item1"); | ||
assert.equal(value, 1); | ||
done(); | ||
}, 2000); | ||
// wait 6 seconds, then try to read the file, should be unfined | ||
// wait 5.5 seconds, then try to read the item1 file, should be undefined | ||
setTimeout(function() { | ||
var value = storage.getItemSync("item1"); | ||
assert.equal(value, undefined); | ||
// call done only once here, since it's 3 seconds after | ||
done(); | ||
}, 6000); | ||
}, 5500); | ||
}); | ||
it("should automatically delete expired items", function(done) { | ||
this.timeout(10000); | ||
var storage = nodePersist.create(); | ||
storage.initSync({ | ||
dir: randDir(), | ||
expiredInterval: 3000 | ||
}); | ||
storage.setItemSync("item1", 1, {ttl: 5000}); | ||
// wait 8 seconds, then check the keys, should be empty, item1 should've been deleted automatically based on the expiredInterval | ||
setTimeout(function() { | ||
var value = storage.keys(); | ||
assert.equal(value.length, 0); | ||
done(); | ||
}, 7000); | ||
}); | ||
@@ -334,0 +354,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49147
1000