First of all, what is a cache? A cache is a place where frequently-accessed data is stored that can be quickly retrieved with little overhead. Cache is useful when you have a database-driven website that gets high amounts of traffic as it can help reduce the load on your database server by bypassing it completely to retrieve certain data.

The way caching works is very simple. When a piece of data is needed, your code first checks to see if it exists in your cache. If the data you need does not exist in the cache, it will then get the data from the database. A copy of that data is then placed in the cache so it will be available there the next time you ask for that particular piece of data. That copy of the data will sometimes have a TTL or (time to live) expiration time. This ensures that data in the cache does not get outdated as updates are made to the original record in the database.

A cache is usually stored in memory so that it can be accessed quickly and with less technical overhead. You can pull data from RAM much faster than from a database, but the data will not be persistent (will not be saved if the process is stopped). This makes sense for caching since we want to access data faster, but there is a saved copy of that data in the database.

When you hear the term "stored in memory" it means the value is stored in the server's RAM. Storing something in the server's RAM can be as simple as setting a variable equal to a value. Since a running NodeJS process/script stores variable data in RAM, you are storing that data in memory.

LRU cache stands for "least recently used" cache. This puts a limit on the amount of items you store in the cache your server does not run out of memory as more and more items are stored in the cache. As the limit of items is reached, the LRU cache will start to remove the "least recently used" items in the cache.

Let's learn how to use the NodeJS LRU cache module in an app. For the purposes of this tutorial, I will skip the details of accessing the data from the database.

Installing The LRU Cache Node Module

To begin, we will install the LRU cache module via NPM. Navigate to your app's root directory (the same one with the package.json file) in console and type the following command:

$ npm install --save lru-cache

This will install the lru-cache module into the node_modules folder for your app.

Importing the Module

Next, in we will import the module into our application and create our cache while setting a few options...

var cache = require('lru-cache')({  
    max : 100,                   // The maximum number of items allowed in the cache
    max_age : 1000 * 60 * 60     // The maximum life of a cached item in milliseconds

We only want a maximum of 100 items to be in our cache at any given time and each item can only stay in the cache for 1 hour.

Storing and Retrieving Data

Now we can start using our cache to store and retrieve data. It makes sense to create a reusable library when we setup our cache so we don't have to write the logic every time we access data, but for the purposes of demonstration, we won't get into that detail here.

function getData(table, id) {  
    // Create a key to identify the data in the cache
    var cache_key = table + ':' + id;

    // Check if the data is in the cache
    var data = cache.get(cache_key);

    // The key was not in the cache so data is undefined
    if (!data) {
        // Get the data from the database
        data = getDataFromDatabase(table, id);

        // Store the data in the cache
        cache.set(cache_key, data);

    return data;

// Retrieve a piece of data
var user = getData('users', '12345');  

It's as simple as that. With this little bit of code, we have effectively reduced the amount of strain on our database server.