Kyle Davis wrote this beautiful article on how to store objects in Redis.
I have seen at least one module inspired by this article and this is yet another.
This module inproves on the concept in sevaral important ways.
-
Kyle talks of the shortcomings of flat and I have used dot-object which has a more superior API. Check it out.
-
I wanted to extend the ability to fetch flattened keys i.e.
'key1.nestedkey.nestedkey2'
to being able to fetch partial keys like'key1.nestedkey.nestedkey2'
. dot-object module provides a useful method'.pick'
and we utilize it for such. -
Finally, I wanted to have the ability to fetch multiple keys at once and return a well formated partial object. Example, for an object like :
{k1:1, kn:{kn2:{kn3: 300 }}, k3:3, k4:4}
, it would be nice to query for['k1','kn.kn2']
and get an object with only those values. Right?
Install npm install -s redis-odb
. Then go ahead and set/get objects.
const fs = require('fs');
const redisODB = require('.');
//lets use this package.json as our object
var obj = JSON.parse( fs.readFileSync('./package.json').toString() ) ;
//Set Options
let options= {
host : '127.0.0.1',
port : '6379',
db : 3
}
//redis Hash Key
var key = 'TEST';
//Add Data to ODB
redisODB.set(key,obj, function(err, res){
console.log(JSON.stringify(res,0,4));
console.log('\n');
}, options );
//get a subset of the object keys....
var searchKey = ['keywords', 'dependencies' ] ;
//Get Data from ODB
redisODB.get(key, searchKey, function(err, res){
console.log(JSON.stringify(res,0,4));
}, options );
Outputs a partial object:
{
"keywords": [
"redis",
"object",
"database",
"db"
],
"dependencies": {
"dot-object": "^1.5.4",
"async": "^2.1.4",
"redis": "^2.6.3"
}
}
There are only two methods that you should worry about.
Save object into redis. Requires a string value that acts as the HASH KEY in redis.
Retrieve object from redis. Requires a string value that acts as the HASH KEY in redis.
These are the same options that you would pass to node-redis.
- Better management of redis connections. Should we close connections after every query or keep connections alive?