express-session full featured
MemoryStoremodule without leaks!
A session store implementation for Express using lru-cache.
Because the default MemoryStore for express-session will lead to a memory leak due to it haven't a suitable way to make them expire.
The sessions are still stored in memory, so they're not shared with other processes or services.
$ npm install express-session memorystore
Pass the express-session store into memorystore to create a MemoryStore constructor.
const session = require('express-session')
const MemoryStore = require('memorystore')(session)
app.use(session({
cookie: { maxAge: 86400000 },
store: new MemoryStore({
checkPeriod: 86400000 // prune expired entries every 24h
}),
resave: false,
secret: 'keyboard cat'
}))
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 lengthInfinity.ttl Session TTL (expiration) in milliseconds. Defaults to session.maxAge (if set), or one day. This may also be set to a function of the form (options, sess, sessionID) => number.dispose Function that is called on sessions when they are droppedkey, value. It's called beforenextTick or setTimeout callback or it won't do anything.stale By default, if you set a maxAge, it'll only actually pullget(key). (That is, it'ssetTimeout or anything.) If you setstale:true, it'll return the stale value before deleting it. Ifundefined when you try tonoDisposeOnSet By default, if you set a dispose() method, then it'll be called whenever a set() operation overwrites an existing key. If you set this option, dispose() will only be called when a key falls out of the cache, not when it is overwritten.serializer An object containing stringify and parse methods compatible with Javascript's JSON to override the serializer used.memorystore implements all the required, recommended and optional methods of the express-session 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.
To enable debug set the env var DEBUG=memorystore
Rocco Musolino (@roccomuso)
MIT