Skip to content

Instantly share code, notes, and snippets.

@DTrejo
Forked from slaskis/test.redis-multi.js
Created February 24, 2013 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DTrejo/5022684 to your computer and use it in GitHub Desktop.
Save DTrejo/5022684 to your computer and use it in GitHub Desktop.
var redis = require('redis')
, client = redis.createClient()
, assert = require('assert');
// redis.debug_mode = true;
client.on("error", function (err) {
console.log("Error " + err);
});
function gen(i){
var s = '';
while(i--)
s += 'k'//String.fromCharCode(Math.random()*100+100);
return s;
}
// generate 1Mb of hash
var hash1024 = {};
for(var l=100; l> 0; l--){
// 1kb key, 9kb value
hash1024[gen(1024)] = gen(1024*9);
}
// generate a hash
var hash100 = {};
for(var l=100; l> 0; l--){
// 1b key, 1kb value
hash100['k'] = 'v';
}
// select other db
client.select(2);
// clean out old keys
var multi = client.multi();
multi.del('hi1')
multi.del('hi2')
multi.del('hi3')
multi.exec(function(err,results){
assert.ifError(err)
// this will fail on first run.
// assert.equal(results[0],'1')
// assert.equal(results[1],'1')
// assert.equal(results[2],'1')
});
// select hash (large: 1024 or small: 100)
var hash = hash100;
// hmset
var multi = client.multi();
multi.hmset('hi1',hash);
multi.hmset('hi2',hash);
multi.hmset('hi3',hash);
multi.exec(function(err,results){
assert.ifError(err)
assert.equal(results[0],'OK')
assert.equal(results[1],'OK')
assert.equal(results[2],'OK')
// hmget
// watch out for QUEUED
var multi = client.multi();
multi.hgetall('hi1')
multi.hgetall('hi2')
multi.hgetall('hi3')
multi.exec(function(err,results){
assert.ifError(err);
console.log(results)
assert(results[0],'could not find result 0')
assert(results[1],'could not find result 1')
assert(results[2],'could not find result 2')
Object.keys(hash).forEach(function(k){
assert(results[0][k],'could not find '+k+' in result 0')
assert(results[1][k],'could not find '+k+' in result 1')
assert(results[2][k],'could not find '+k+' in result 2')
assert.equal(results[0][k],hash[k])
assert.equal(results[1][k],hash[k])
assert.equal(results[2][k],hash[k])
})
client.quit()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment