memorystore
Advanced tools
Comparing version 1.4.2 to 1.5.0
@@ -15,3 +15,3 @@ /*! | ||
var oneDay = 3600000 | ||
var oneDay = 86400000 | ||
@@ -30,2 +30,3 @@ function getTTL (options, sess, sid) { | ||
function prune (store) { | ||
debug('Pruning expired entries') | ||
store.forEach(function (value, key) { | ||
@@ -71,4 +72,3 @@ store.get(key) | ||
this.options = {} | ||
this.options.checkPeriod = (typeof options.checkPeriod !== 'undefined') | ||
? options.checkPeriod : oneDay | ||
this.options.checkPeriod = options.checkPeriod | ||
this.options.max = options.max || Infinity | ||
@@ -260,14 +260,12 @@ this.options.ttl = options.ttl | ||
* Start the check interval | ||
* @param {Number} ms | ||
* @api public | ||
*/ | ||
MemoryStore.prototype.startInterval = function (Ms) { | ||
clearInterval(this._checkInterval) | ||
MemoryStore.prototype.startInterval = function () { | ||
var self = this | ||
var ms = Ms || this.options.checkPeriod | ||
var ms = this.options.checkPeriod | ||
if (ms && typeof ms === 'number') { | ||
clearInterval(this._checkInterval) | ||
debug('starting periodic check for expired sessions') | ||
this._checkInterval = setInterval(function () { | ||
debug('Pruning old entries') | ||
prune(self.store) // iterates over the entire cache proactively pruning old entries | ||
@@ -288,3 +286,12 @@ }, Math.floor(ms)) | ||
/** | ||
* Remove only expired entries from the store | ||
* @api public | ||
*/ | ||
MemoryStore.prototype.prune = function () { | ||
prune(self.store) | ||
} | ||
return MemoryStore | ||
} |
{ | ||
"name": "memorystore", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "express-session full featured MemoryStore layer without leaks!", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -22,3 +22,5 @@ # memorystore [](https://www.npmjs.com/package/memorystore) [](https://travis-ci.org/roccomuso/memorystore) [](https://david-dm.org/roccomuso/memorystore) [](https://standardjs.com) | ||
app.use(session({ | ||
store: new MemoryStore(options), | ||
store: new MemoryStore({ | ||
checkPeriod: 86400000 // prune expired entries every 24h | ||
}), | ||
secret: 'keyboard cat' | ||
@@ -30,3 +32,3 @@ })) | ||
* `checkPeriod` Define how long MemoryStore will check for expired. The period is in ms. By default every 24 hour. Pass `0` or `false` to disable the automatic check. | ||
* `checkPeriod` Define how long MemoryStore will check for expired. The period is in ms. The automatic check is disabled by default! Not setting this is kind of silly, since that's the whole purpose of this lib. | ||
* `max` The maximum size of the cache, checked by applying the length | ||
@@ -52,4 +54,8 @@ function to all values in the cache. It defaults to `Infinity`. | ||
`memorystore` implements all the **required**, **recommended** and **optional** methods of the [express-session](https://github.com/expressjs/session#session-store-implementation) store. Plus a `startInterval()` and `stopInterval()` methods to start/clear the automatic check for expired. | ||
`memorystore` implements all the **required**, **recommended** and **optional** methods of the [express-session](https://github.com/expressjs/session#session-store-implementation) store. Plus a few more: | ||
- `startInterval()` and `stopInterval()` methods to start/clear the automatic check for expired. | ||
- `prune()` that you can use to manually remove only the expired entries from the store. | ||
## Debug | ||
@@ -56,0 +62,0 @@ |
@@ -16,8 +16,7 @@ var assert = require('assert') | ||
var store = this.store | ||
var oneDay = 3600000 | ||
assert.ok(store.options, 'should have an option object') | ||
assert.equal(store.options.max, Infinity, 'max option should be Infinity') | ||
assert.equal(store.options.checkPeriod, oneDay, 'checkPeriod equal to ' + oneDay + ' by default') | ||
assert.equal(store.options.checkPeriod, undefined, 'checkPeriod undefined by default') | ||
assert.ok(store.store, 'should have the LRU cache store') | ||
assert.ok(store._checkInterval, 'should have the pruning loop') | ||
assert.equal(store._checkInterval, undefined, 'should not have the pruning loop') | ||
done() | ||
@@ -43,2 +42,9 @@ }) | ||
it('should not set the interval to check for expired entries by default', function (done) { | ||
this.store = new session.MemoryStore() | ||
var store = this.store | ||
assert.equal(store._checkInterval, undefined, 'should not exists') | ||
done() | ||
}) | ||
it('should only contain 10 items', function (done) { | ||
@@ -176,3 +182,3 @@ this.store = new session.MemoryStore({max: 10}) | ||
it('should prune expired entries', function (done) { | ||
it('should enable automatic prune for expired entries', function (done) { | ||
this.store = new session.MemoryStore({checkPeriod: 300}) | ||
@@ -196,4 +202,4 @@ var store = this.store | ||
it('should disable automatic check for expired', function (done) { | ||
this.store = new session.MemoryStore({checkPeriod: false}) | ||
it('automatic check for expired entries should be disabled', function (done) { | ||
this.store = new session.MemoryStore() | ||
var store = this.store | ||
@@ -200,0 +206,0 @@ |
21381
500
69