• Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Try NCache
  • Contact Us
Try NCache
Show / Hide Table of Contents

Configure Node.js Sessions

NCache provides a session store for Express applications through the ncache-sessions package. This page explains how to configure the store and register it with express-session so that session data is shared across your web application instances.

Important

Ensure the following development tools are installed on your Linux system when using NCache Node.js:

  • g++
  • make

Prerequisites

  • Node.js
  • Install and include the following module in your application based on your NCache edition:
    • NCache: ncache-sessions
  • Install Express and express-session.
  • Include the following classes in your application:
    • Cache
  • The cache must be running.
  • For API details, refer to: Cache.

Configure the NCache Session Store

NCache has to be configured in the Node.js application's config.json file by providing the cacheName and other related properties. Following is an explanation of a sample json file.

{
    "ncacheStore": {
        "ttl" : 3600,
        "cacheName" : "defaultCache",
        "disableTouch" : false,
        "sessionTag" : "sess"
    }
}

The properties added in the file are explained below:

  1. cacheName: The name of the cache in use.
  2. ttl: Also called time-to-live, this is the total time for which the session would remain in the NCache store.
  3. sessionTag: This is the tag associated with the session that you can use to identify a session.
  4. disableTouch: This is set to False by default, which means that the session is being refreshed every time you try to access it.
Note
  • If ttl, sessionTag, or disableTouch is omitted, the NCache session store uses its defaults.
  • The user can retrieve their data by using GetByTags in NCache Tags API.

Configure Express Session Middleware

Create the NCache store and pass it to the express-session middleware in your express.js file.

  • Node.js
const express = require('express');
const session = require('express-session');
const NCacheStore = require('ncache-sessions')(session);
const app = express();
const config = require('./config.json');

async function configureSessions() {
    // Create store for NCache
    const store = await NCacheStore.createStore(config.ncacheStore);

    // Configure session middleware
    app.use(session({
        secret: 'keyboard cat',
        resave: false,
        saveUninitialized: true,
        store: store
    }));
}

configureSessions().catch(console.error);
Note

To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.

The express-session middleware uses the created NCache store for session persistence. Register the middleware before adding routes that access session data.

See Also

Tag Properties
Error Logging

Contact Us

PHONE

+1 214-619-2601   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Try NCache
Pricing
NCache Playground

Deployments
  • Cloud
  • Docker
  • Kubernetes
  • On-Premises
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • SignalR Backplane
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Blogs
  • Foundations
  • Videos
  • Comparisons
  • Shows
  • Whitepapers
  • Articles
  • Tech Talks
  • Magazine Articles
  • Third-Party Articles
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Docs
  • Schedule a Demo
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Terms of Use
  •  
  • Privacy Policy
  •  
  • Cookie Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
  • Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS