Rather than use the likes of Redis, or even LokiJS (which looks great), can I just create a large javascript collection in memory as my app runs, and query that instead?
Asked
Active
Viewed 483 times
3
-
yes, there is nothing bad on it. – webduvet Oct 30 '15 at 11:35
-
2This has a very good answer to your question: http://stackoverflow.com/questions/19477821/redis-cache-vs-using-memory-directly – Wylie Kulik Oct 30 '15 at 12:37
1 Answers
0
I have an app with this exact pattern using socket.io.
Bellow, I have translated my socket.io code to use HTTP Requests.
On the server, you can do something like that:
var players = []; //This will be used to store in-memory players.
Them,
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); //Need this to populate req.body on requests.
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
next(); //Need this to accept incoming requests.
});
app.get('/players', function (req, res) {
res.json(players);
});
app.post('/players', function (req, res) {
//validation
players.push(req.body);
});
[...]
Look at this example: http://agar.io/
This is a very popular game. Now, why would they store your position, score or name, while playing, on database? This would be very costly.

Rodmentou
- 1,610
- 3
- 21
- 39