Registry / storage / egg-session-redis

egg-session-redis

JSON →
library2.1.0jsnpmunverified

Redis-based session store plugin for the Egg.js framework (v2.1.0, stable). Designed to persist session data in Redis, leveraging the egg-redis plugin. Requires both egg-redis and egg-session-redis to be enabled. Supports single and multiple Redis instances with configurable instance selection. Minimal configuration; works out-of-the-box with a single Redis client. Active maintenance, used in production by many Egg.js projects. Key differentiator: seamless integration with Egg.js lifecycle, no boilerplate.

npm install egg-session-redis
INSTALL
IMPORT
SIG · EGG-SESSION-REDIS
E
egg-session-redis
storagejavascriptv2.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

plugin config
exports.sessionRedis = { enable: true, package: 'egg-session-redis' };
const sessionRedis = require('egg-session-redis');
Used as an Egg.js plugin, not imported directly. Enable in config/plugin.js.
sessionRedis config
exports.sessionRedis = { name: 'session' };
exports.sessionRedis = {};
Only needed when using multiple Redis clients. Single client needs no config.
egg-redis config
exports.redis = { client: { host: '...', port: 6379, password: '', db: 0 }, agent: true };
exports.redis = {};
Must configure at least one Redis client for the session store to work.

Shows how to enable and configure egg-session-redis with a single Redis client, then use session in a route.

// config/plugin.js exports.redis = { enable: true, package: 'egg-redis', }; exports.sessionRedis = { enable: true, package: 'egg-session-redis', }; // config/config.default.js exports.redis = { client: { host: process.env.REDIS_HOST ?? '127.0.0.1', port: Number(process.env.REDIS_PORT) ?? 6379, password: process.env.REDIS_PASSWORD ?? '', db: 0, }, agent: true, }; // app/router.js module.exports = app => { const { router, controller } = app; router.get('/', async ctx => { ctx.session.visits = ctx.session.visits ? ctx.session.visits + 1 : 1; ctx.body = `Visits: ${ctx.session.visits}`; }); };
Debug
Known issues
breakingIf using multiple Redis clients, you must specify which client to use for session via exports.sessionRedis.name. Failure to do so will cause sessions to not be stored correctly, leading to session persistence issues.
fix
Add exports.sessionRedis = { name: 'yourSessionClientName' } in config.
affects: >=2.0.0
gotchaThe egg-redis configuration requires 'agent: true' (or 'agent: false') property. For session storage in multi-process apps, agent: true is recommended to avoid connection duplication.
fix
Set agent: true in redis client config.
affects: >=2.0.0
deprecatedSession keys might not be automatically cleaned up if TTL is not configured. The default session TTL is 24 hours by Egg.js, but ensure your Redis eviction policy doesn't conflict.
fix
Monitor Redis memory usage and configure session TTL via Egg.js session config if needed.
affects: >=1.0.0
Errors
Common errors & fixes
Error: [egg-redis] missing `config.redis`
egg-redis plugin is not configured or enabled before egg-session-redis.
fix
Ensure both plugins are enabled in config/plugin.js and provide redis config in config.default.js.
Error: Can't find plugin egg-redis
egg-redis npm package not installed.
fix
Run: npm install egg-redis
TypeError: Cannot read property 'setex' of undefined
Redis client not initialized because config.redis.client is missing or invalid.
fix
Check your redis config in config.default.js: ensure host, port, password, db are correct.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
egg-redisrequiredegg-session-redis is an extension that stores session data in Redis; it requires the egg-redis plugin to connect to Redis.
eggoptionalEgg.js framework required to load the plugin.
Agent activity
32 hits · last 30 days
node
30
Resources
egg-session-redis — npm install egg-session-redis · libregistry