user2572969
user2572969

Reputation: 267

Implenting local storage for each request in node.js

I am Instrument node.js application, for these I need to find out which method belongs to which request, so for these I have maintained unique id for each request, but where should I store this id so that it is accessible for all methods and it should be separate for each request. I need something like thread-local concept of java in nodejs,

Upvotes: 1

Views: 448

Answers (1)

baisang
baisang

Reputation: 434

Try something like node-cache

It's pretty easy to use

var NodeCache = require('NodeCache');
var myCache = new NodeCache();
var obj = {'some': 'object'};
myCache.set('key', obj);
var cached = myCache.get('key');

It won't work if you have a Node Cluster I believe. In that case you'll have to use an external system like Redis.

Of course, Javascript objects themselves can be used as associative arrays:

var associativeArray = {};
associativeArray.foo = 'bar';
associativeArray['bar'] = associativeArray['foo'];

Upvotes: 1

Related Questions