Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that executes JavaScript code outside a web browser for easily building fast, scalable network applications.

NCache, as we know, an in-memory, distributed, and linearly scalable cache, stores data for better performance and high scalability. NCache, a native .NET cache that does wonders when it comes to fast access of data for your application’s increased performance, is now expanding its usage by supporting Node.js client. Node.js can now be used with NCache Client-Side API which includes basic operations, data expiration techniques and locking, etc. along with Session Caching.

NCache Details                                                             NCache Docs                                                           Client-Side Features Docs

Why Use Node.js with NCache?

NCache with the support of Node.js along with its vast features and enhanced caching capabilities gives flexible options for you to use Node.js as easily as possible. If you want your Node.js application to run in a load-balanced environment with better performance, NCache comes with the following advantages:

  • Better Performance: Data is stored in an in-memory cache that is closer to the Node.js application, yielding maximum performance and faster response time.
  • Reliability: If an application server goes down or somehow data is lost, NCache provides reliability where the data remains available.
  • Scalability: NCache provides linear scalability by allowing you to add more cache servers when the transaction load grows. For example, while using your Node.js application, you can instantly add a new cache server depending on the situation and continue to serve more requests without having to stop your application.
  • Availability and Fault Tolerance: While using NCache with Node.js application in a web farm, NCache provides better fault tolerance by keeping the data available at all server nodes in a clustered cache with no single point of failure.
  • Cluster Topologies: NCache offers different caching topologies to make your Node.js application more reliable and scalable. For example, Replicated Topology provides load balancing so that your application does not suffer intensive traffic and Partitioned-Replica Topology provides higher reliability and scalability for faster transactions.  

NCache Details                                                          Caching Topologies                                                  NCache Architecture

Node.js Client-Side Features of NCache

NCache has a variety of Client-Side features specially made for you. Node.js can be used with those features alongside their APIs and other methods. In this blog, I’ll discuss some of the features along with their implementation for a better understanding of caching data with Node.js.

Before using the below features, you need to install the npm package named ncache-client required for using NCache client with Node.js. This package installs all the required client libraries for your Node.js application. Type the following command in the terminal section of Visual Studio Code:

Next, include the following module in your Node.js application to get started.

Basic Operations for Caching Data

NCache provides various CRUD APIs for caching data using Node.js. The data can be a single item or bulk and can be used synchronously to add, update or remove from the cache. Following is the implementation to add data in the cache with a custom object using Node.js API add().

Tag Cache Data

NCache provides Tags, with which you can associate keywords with your cache items for performing various operations on the data for efficient results. Given below is the implementation to add data with tags using the setTags() property.

Data Expiration Strategies

NCache supports time-based data expiration strategies, which are Absolute Expiration and Sliding Expiration, where you specify the time interval to expire the cached data. Following is the implementation for adding/updating cache items with absolute expiration.

Locking in NCache                                                    Pub/Sub with NCache                                                    SQL Search in Cache

Node.js Session Caching with NCache

NCache provides a store for session data storage in Node.js which uses express-session middleware to manage sessions. If your application is running in a web server farm and you need reliable and scalable storage for your session persistence, then the answer is NCache! Hence, I’ll discuss how to configure NCache for your Node.js sessions.

Step:1 Configure NCache in Config.json

First, NCache needs to be configured in the Node.js application’s config.json file by providing cache name and other related properties like shown below.

Step:2 Configure NCache Store and Session Middleware in Express.js

Next, configurations must be done in the express.js file where session middleware is created along with NCache as a custom store to be used for data storage.

After configuring NCache as a data store, you are now ready to use Node.js sessions with NCache as your session persistence store. For a thorough understanding, refer to Node.js Session Caching Docs.

Concluding the Blog!

NCache, being a distributed caching solution, supports Node.js to be used seamlessly along with its Client-Side APIs and their methods. NCache boosts the speed, enhances the performance, and gives reliability along with scalability. So, be ready and use NCache now!

NCache Details                                                            Download NCache                                                            Edition Comparison

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment