I recently switched from memcached to redis in nodejs. The thing I liked in node-memcached was that I can save the whole javascript object in the memory. Sadly I couldn't do this in redis. For example, I got the following object:
var obj = {
name: "Hello world!",
author: "admin",
user: {
"yolololo" : {
"id": "352asdsafaseww",
"server": 5,
"data" : {
x: 1,
y: 1,
z: 50
}
},
"yolol" : {
"id": "358dsa",
"server": 7
}
}
}
with the 3rd-Eden/node-memcached I could just do:
memcached.set("obj", obj, 12345, function(err) { });
and then
memcached.get("obj", function(err, data) {
console.log(data);
});
And I'll get the object I saved, just the way it is.
The problem with redis is that if I save the object like this:
redisclient.set("obj", obj, redis.print);
When I get the value with
redisclient.get("obj", function(err, data) {
console.log(data);
});
The output is just string containing [object Object]
.
Yeah I understand redis is text-based protocol and it's trying to do obj.toString(), but seems memcached take care of objects and redis don't. I thought I could just do:
redisClient.set("obj", JSON.stringify(obj));
but I'm not sure if this will be good, because there will be insane high I/O and I'm not sure if the JSON obj->string will be bottleneck ( 10k+ request/second ).
Both Memcached and Redis store the data as string, but does redis have built-in feature for converting objects?
I recently switched from memcached to redis in nodejs. The thing I liked in node-memcached was that I can save the whole javascript object in the memory. Sadly I couldn't do this in redis. For example, I got the following object:
var obj = {
name: "Hello world!",
author: "admin",
user: {
"yolololo" : {
"id": "352asdsafaseww",
"server": 5,
"data" : {
x: 1,
y: 1,
z: 50
}
},
"yolol" : {
"id": "358dsa",
"server": 7
}
}
}
with the 3rd-Eden/node-memcached I could just do:
memcached.set("obj", obj, 12345, function(err) { });
and then
memcached.get("obj", function(err, data) {
console.log(data);
});
And I'll get the object I saved, just the way it is.
The problem with redis is that if I save the object like this:
redisclient.set("obj", obj, redis.print);
When I get the value with
redisclient.get("obj", function(err, data) {
console.log(data);
});
The output is just string containing [object Object]
.
Yeah I understand redis is text-based protocol and it's trying to do obj.toString(), but seems memcached take care of objects and redis don't. I thought I could just do:
redisClient.set("obj", JSON.stringify(obj));
but I'm not sure if this will be good, because there will be insane high I/O and I'm not sure if the JSON obj->string will be bottleneck ( 10k+ request/second ).
Both Memcached and Redis store the data as string, but does redis have built-in feature for converting objects?
Share Improve this question edited Sep 22, 2013 at 9:12 Deepsy asked Sep 22, 2013 at 9:04 DeepsyDeepsy 3,8108 gold badges43 silver badges72 bronze badges 2- I think memcached stores data as string. node-memcached does (de)serialization process automatically. – fardjad Commented Sep 22, 2013 at 9:09
- @fardjad Yeah, you're right, but does Redis have such build-in process or I need to convert it manually? – Deepsy Commented Sep 22, 2013 at 9:11
1 Answer
Reset to default 14First of all redis only supports the following data types:
- String
- List
- Set
- Hash
- Sorted set
You'll need to store objects as string in both redis and memcached.
node-memcached parses/stringifies the data automatically. But node-redis doesn't.
However, you can implement your own serialization/deserialization functions for your app.
The way node-memcached stringifies an object is as follows:
if (Buffer.isBuffer(value)) {
flag = FLAG_BINARY;
value = value.toString('binary');
} else if (valuetype === 'number') {
flag = FLAG_NUMERIC;
value = value.toString();
} else if (valuetype !== 'string') {
flag = FLAG_JSON;
value = JSON.stringify(value);
}
It also parses the retrieved text this way:
switch (flag) {
case FLAG_JSON:
dataSet = JSON.parse(dataSet);
break;
case FLAG_NUMERIC:
dataSet = +dataSet;
break;
case FLAG_BINARY:
tmp = new Buffer(dataSet.length);
tmp.write(dataSet, 0, 'binary');
dataSet = tmp;
break;
}