Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Object In Memory Between Requests with SailsJS/ Express

I'm building a server using SailsJS (a framework built on top of Express) and I need to keep an object in memory between requests. I would like to do this because loading it to/ from a database is taking way too long. Any ideas how I could do this?

Here's my code:

var params = req.params.all();

Network.findOne({ id: params.id }, function(err, network) {
  if(network) {
    var synapticNetwork = synaptic.Network.fromJSON(network.jsonValue);
    if(synapticNetwork) { ...

Specifically, the fromJSON() function takes way too long and I would rather keep the synapticNetwork object in memory while the server is running (aka. load it when the server starts and just save periodically).

like image 823
Rob Avatar asked Mar 20 '15 17:03

Rob


People also ask

Can express handle multiple requests?

Express. js use different kinds of middleware functions in order to complete the different requests made by the client for e.g. client can make get, put, post, and delete requests these requests can easily handle by these middleware functions.

Does express cache automatically?

Yes, express handles cache-control automatically. It's default value is set to true. And you can just handle it by increasing/decreasing it's maxAge property value. ( in milliseconds ).

Can NodeJS handle multiple requests?

How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.

How many requests NodeJS can handle?

As is, node. js can process upwards of 1000 requests per second and speed limited only to the speed of your network card. Note that it's 1000 requests per second not clients connected simultaneously. It can handle the 10000 simultaneous clients without issue.


2 Answers

There are plenty libraries out there for caching purposes, one of which is node-cache as you've mentioned. All of them share similar api :

var cache = require('memory-cache');

// now just use the cache

cache.put('foo', 'bar');
console.log(cache.get('foo'))

You can also implement your own module and just require it wherever you need:

var cache = {};

module.exports = {
    put: function(key, item) {
        cache[key] = item;
    },
    get: function(key) {
        return cache[key];
    }
}
like image 76
Vsevolod Goloviznin Avatar answered Oct 17 '22 14:10

Vsevolod Goloviznin


There are a lot of potential solutions. The first and most obvious one is using some session middleware for express. Most web frameworks should have some sort of session solution.

https://github.com/expressjs/session


The next option would be to use a caching utility like what Vsevolod suggested. It accomplishes pretty much the same thing as session, except if the data needs to be tied to a user/session then you'll have to store some kind of identifier in the session and use that to retrieve from the cache. Which I think is a bit redundant if that's your use-case.

There are also utilities that will expand your session middle-ware and persist objects in session to a database or other kinds of data stores, so that session information isn't lost even after server restarts. You still get the speed of an in-memory store, but backed by a database in case the in-memory store gets blown away.


Another option is to use Redis. You still have to serialize/deserialize your objects, but Redis is an in-memory data store and is super quick to write to and read from.

like image 45
Chev Avatar answered Oct 17 '22 13:10

Chev