What is memorystore?
The 'memorystore' npm package is a session store for Express.js that uses a memory-based storage system. It is designed to be a simple and efficient way to store session data in memory, which can be useful for development and testing environments where persistence is not required.
What are memorystore's main functionalities?
Session Store Initialization
This code sample demonstrates how to initialize a session store using 'memorystore' in an Express.js application. The store is configured to prune expired entries every 24 hours, and sessions are set up with a maximum age of 24 hours.
const session = require('express-session');
const MemoryStore = require('memorystore')(session);
const store = new MemoryStore({
checkPeriod: 86400000 // prune expired entries every 24h
});
app.use(session({
cookie: { maxAge: 86400000 },
store: store,
resave: false,
saveUninitialized: true,
secret: 'your-secret-key'
}));
Other packages similar to memorystore
connect-redis
The 'connect-redis' package is a Redis-based session store for Express.js. It is suitable for production environments where session data needs to be persisted across server restarts. Unlike 'memorystore', which stores data in memory, 'connect-redis' uses Redis, a fast, in-memory data structure store, to persist session data.
express-session
While 'express-session' is a middleware for managing sessions in Express.js, it does not provide a storage mechanism by itself. It can be used with various session stores, including 'memorystore'. 'express-session' is more of a foundational package that requires a store like 'memorystore' or others to handle session data storage.
connect-mongo
The 'connect-mongo' package is a MongoDB-based session store for Express.js. It is ideal for applications that already use MongoDB as their database, allowing session data to be stored alongside other application data. Unlike 'memorystore', 'connect-mongo' provides persistent storage, making it suitable for production use.
memorystore

express-session full featured MemoryStore
module 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.
Setup
$ npm install express-session memorystore
Pass the express-session
store into memorystore
to create a MemoryStore
constructor.
var session = require('express-session')
var MemoryStore = require('memorystore')(session)
app.use(session({
store: new MemoryStore({
checkPeriod: 86400000
}),
secret: 'keyboard cat'
}))
Options
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
function to all values in the cache. It defaults to Infinity
.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 dropped
from the cache. This can be handy if you want to close file
descriptors or do other cleanup tasks when sessions are no longer
accessible. Called with key, value
. It's called before
actually removing the item from the internal cache, so if you want
to immediately put it back in, you'll have to do that in a
nextTick
or setTimeout
callback or it won't do anything.stale
By default, if you set a maxAge
, it'll only actually pull
stale items out of the cache when you get(key)
. (That is, it's
not pre-emptively doing a setTimeout
or anything.) If you set
stale:true
, it'll return the stale value before deleting it. If
you don't set this, then it'll return undefined
when you try to
get a stale entry, as if it had already been deleted.serializer
An object containing stringify
and parse
methods compatible with Javascript's JSON
to override the serializer used.
Methods
memorystore
implements all the required, recommended and optional methods of the express-session store. Plus a few more:
Debug
To enable debug set the env var DEBUG=memorystore
Author
Rocco Musolino (@roccomuso)
License
MIT