node-persist
Advanced tools
Comparing version 2.0.2 to 2.0.3
{ | ||
"name": "node-persist", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Super-easy (and fast) persistent data structures in Node.js, modeled after HTML5 localStorage", | ||
@@ -5,0 +5,0 @@ "main": "./src/node-persist.js", |
@@ -135,3 +135,3 @@ # node-persist | ||
#### `setItem(key, value, [callback])` - asynchronous*, returns Promise | ||
#### `setItem(key, value, [options, callback])` - asynchronous*, returns Promise | ||
This function sets 'key' in your database to 'value'. It also sets a flag, notifying that 'key' has been changed and needs to be persisted in the next sweep. Because the flag must be set for the object to be persisted, it is best to use node-persist in a functional way, as shown below. | ||
@@ -144,2 +144,5 @@ | ||
}); | ||
storage.setItem(42,'the answer to life, the universe, and everything.', {ttl: 1000*60 /* 1 min */ }, function(err) { | ||
// done | ||
}); | ||
@@ -153,3 +156,3 @@ var batman = storage.getItem('batman'); | ||
// success | ||
}, | ||
}, | ||
function() { | ||
@@ -159,7 +162,12 @@ // error | ||
``` | ||
\* `setItem()` is asynchronous, however, depending on your global options, the item might not persist to disk immediately, so, if you set `options.interval` or `options.continuous=false`, your (optional) callback or your returned promise from this function will get called/resolved immediately, even if the value has not been persisted to disk yet, which could be either waiting for the interval to kick in or for your manual call to `persist()` | ||
\* The only option available when calling `setItem(key, value, option)` is `{ttl: $milliseconds}` | ||
#### `setItemSync(key, value)` - synchronous, throws Error on failure | ||
If you want to immediately persist to disk, __regardless of the `options.interval` and `options.continuous`__ settings, use this function. | ||
\* `setItem()` is asynchronous, however, depending on your global options, the item might not persist to disk immediately, in the case where you set `options.interval` or `options.continuous=false`, your (optional) callback or your returned promise from this function will still get resolved immediately, even if the value has not been persisted to disk yet, which could be either waiting for the interval to kick in or for your manual call to `persist()` - kind of how the `redis` database works. | ||
#### `setItemSync(key, value, [options])` - synchronous, throws Error on failure | ||
If you want to immediately persist to disk, __regardless of the `this.options.interval` and `this.options.continuous`__ settings, use this function. The only option available when calling `setItemSync(key, value, option)` is `{ttl: $milliseconds}` | ||
```javascript | ||
storage.setItemSync('foo', 'bar'); | ||
storage.setItemSync('hello', 'world', {ttl: 1000 * 60 /* ttl 1 minute */}) | ||
``` | ||
#### `removeItem(key, [callback])` - asynchronous, returns Promise | ||
@@ -166,0 +174,0 @@ This function removes key in the database if it is present, and immediately deletes it from the file system asynchronously. If ttl is used, the corrresponding ttl-key is removed as well |
@@ -192,10 +192,14 @@ /* | ||
set: function (key, value, callback) { | ||
return this.setItem(key, value, callback); | ||
set: function (key, value, options, callback) { | ||
return this.setItem(key, value, options, callback); | ||
}, | ||
setItem: function (key, value, callback) { | ||
setItem: function (key, value, options, callback) { | ||
if (typeof options == 'function') { | ||
callback = options; | ||
options = null; | ||
} | ||
options = options || {}; | ||
callback = isFunction(callback) ? callback : noop; | ||
var options = this.options; | ||
var logmsg = "set (" + key + ": " + this.stringify(value) + ")"; | ||
@@ -206,6 +210,8 @@ | ||
var ttl = options.ttl ? new Date().getTime() + options.ttl : undefined; | ||
// ttl is different that the other options because we can pass a different for each setItem, as well as have a default one. | ||
var ttl = this.calcTTL(options.ttl); | ||
this.data[key] = {value: value, ttl: ttl}; | ||
var result = {key: key, value: value, ttl: ttl, queued: !!options.interval, manual: !options.interval && !options.continuous}; | ||
var instanceOptions = this.options; | ||
var result = {key: key, value: value, ttl: ttl, queued: !!instanceOptions.interval, manual: !instanceOptions.interval && !instanceOptions.continuous}; | ||
@@ -224,3 +230,3 @@ var onSuccess = function () { | ||
if (options.interval || !options.continuous) { | ||
if (instanceOptions.interval || !instanceOptions.continuous) { | ||
this.changes[key] = {onSuccess: onSuccess, onError: onError}; | ||
@@ -244,4 +250,5 @@ } else { | ||
setItemSync: function (key, value) { | ||
var ttl = this.options.ttl ? new Date().getTime() + this.options.ttl: undefined; | ||
setItemSync: function (key, value, options) { | ||
options = options || {}; | ||
var ttl = this.calcTTL(options.ttl); | ||
this.data[key] = {key: key, value: value, ttl: ttl}; | ||
@@ -600,4 +607,13 @@ this.persistKeySync(key); | ||
calcTTL: function (ttl) { | ||
// only check for undefined, if null was passed in setItem then we probably didn't want to use the this.options.ttl | ||
if (typeof ttl == 'undefined') { | ||
ttl = this.options.ttl; | ||
} else { | ||
ttl = ttl ? isNumber(ttl) && ttl > 0 ? ttl : defaultTTL : false; | ||
} | ||
return ttl ? new Date().getTime() + ttl : undefined; | ||
}, | ||
isExpired: function (key) { | ||
if (!this.options.ttl) return false; | ||
return this.data[key] && this.data[key].ttl && this.data[key].ttl < (new Date()).getTime(); | ||
@@ -604,0 +620,0 @@ }, |
@@ -305,2 +305,29 @@ | ||
it("should respect an expired different ttl per setItem and delete the items", function(done) { | ||
var storage = nodePersist.create(); | ||
storage.initSync({ | ||
dir: randDir(), | ||
ttl: 1000 // 1 second | ||
}); | ||
storage.setItemSync("item1", 1, {ttl: 5000}); | ||
// 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 | ||
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 | ||
setTimeout(function() { | ||
var value = storage.getItemSync("item1"); | ||
assert.equal(value, undefined); | ||
done(); | ||
}, 6000); | ||
}); | ||
it("don't persist to disk immediately, but rather on a timely interval", function(done) { | ||
@@ -307,0 +334,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
47083
949
296