azurecache

Winows Azure Cache client and Express session store

Access Windows Azure Cache Service from Node.js

The azurecache module allows you to use the Windows Azure Cache Service to store session state in Express applications. It also allows direct access to Windows Azure Cache Service from other Node.js applications.

The azurecache module uses Edge.js and as such it currently only works on Windows. It is a great fit for storing session state of Express applications hosted in Windows Azure Web Sites. It is also a good choice for any other type of Node.js application hosted in Windows Azure that requires caching.

First create your Windows Azure Cache Service instance following insturctions at Scott Guthrie's blog. You will end up with an endpoint URL of your cache service (e.g. tjanczuk.cache.windows.net) and an access key (a long Base64 encoded string).

Then install the azurecache and express modules:

npm install azurecache
npm install express

Next author your Express application that uses the azurecache module to store Express session state in the Windows Azure Cache Service:

var express = require('express')
    , AzureCacheStore = require('azurecache')(express);
 
var app = express();
 
app.use(express.cookieParser());
app.use(express.session({ store: new AzureCacheStore(), secret: 'abc!123' }));
 
app.get('/inc', function (reqres) {
    req.session.counter = (req.session.counter + 1) || 1;
    res.send(200, 'Increased sum: ' + req.session.counter);
});
 
app.get('/get', function (reqres) {
    res.send(200, 'Current sum: ' + req.session.counter);
});
 
app.listen(process.env.PORT || 3000);

Lastly set some environment variables and start your server:

set AZURE_CACHE_IDENTIFIER=<your_azure_cache_endpoint_url>
set AZURE_CACHE_TOKEN=<your_azure_cache_access_key>
node server.js

Every time you visit http://localhost:3000/inc in the browser you will receive an ever increasing counter value. When you visit http://localhost:3000/get you will receive the current counter value. The value of the counter is stored as part of the Express session state in the Windows Azure Cache Service with a default TTL of one day.

You can specify the credentials to the Windows Azure Cache Service either in code or via environment variables:

var azureCacheOptions = {
    identifier: '<your_endpoint_url>', // or set the AZURE_CACHE_IDENTIFIER environment variable 
    token: '<your_access_key>', // or set the AZURE_CACHE_TOKEN environment variable 
    ttl: 3600 // optional TTL in seconds (default 1 day); or set the AZURE_CACHE_TTL env variable 
};
 
// ... 
 
app.use(express.session({ store: new AzureCacheStore(azureCacheOptions), secret: 'abc!123' }));

You can access Windows Azure Cache Service directly too:

var azurecache = require('azurecache')
 
var cache = azurecache.create({
    identifier: '<your_endpoint_url>', // or set the AZURE_CACHE_IDENTIFIER environment variable 
    token: '<your_access_key>', // or set the AZURE_CACHE_TOKEN environment variable 
    ttl: 3600 // optional TTL in seconds (default 1 day); or set the AZURE_CACHE_TTL env variable 
});
 
cache.put('test1', { first: 'Tomasz', last: 'Janczuk' }, function (error) {
    if (error) throw error;
    cache.get('test1', function (errordata) {
        if (error) throw error;
        console.log('Data from cache:', data);
    });
});

The azurecache module uses Edge.js to access the .NET client of the Windows Azure Cache Service that ships as a NuGet package. The gist of the idea is here.

I do take contributions. Feedback welcome (file an issue). Enjoy!