Node.js + Redis multiple lookups -
i'm new key value stores, i'd learn. personal project, i'm trying build inventory management scheme node.js , redis. let's assume right technology utilize moment.
if had simple system, needs track number of widgets @ particular location, ability details widget or location, understanding according https://matt.sh/thinking-in-redis-part-one store separate "custom indexes" location , item.
in node.js save new entry then, create entry hmset, add together entry 2 indexes sadd:
redis.hmset([ key, 'attr1', entry.attr1, 'attr2', entry.attr2, 'attr3', entry.attr3, ], function(err) { // add together entry location set var locationkey = 'location:' + entry.location; redis.sadd(locationkey, key, function(err) { redis.expire(locationkey, 900); }); // add together entry widget set var widgetkey = 'widget:' + widget.id; redis.sadd(widgetkey, key, function(err) { redis.expire(widgetkey, 900); }); redis.expire(key, 900); } );
now if wanted move widgets 1 location another, we'd need entries widget set, add together entry new location index, , remove old index:
// move widgets location redis.smembers('widget:' + widget.id, function(err, entrykeys) { entrykeys.foreach(function(entrykey) { // entry rebroadcast redis.hgetall(entrykey, function(err, result) { if (result) { // add together entry new location's index var locationkey = 'location:' + location; redis.sadd(locationkey, entrykey, function(err) { redis.expire(locationkey, 900); }); // remove entry old location's index redis.srem('location:' + result.location, entrykey); } }); }); });
my concern number of requests need made each command. adding entry, cost 3 inserts info itself, , 3 more assuming want expire data. moving widgets require 1+n inserts, n reads, , n deletes.
if real time game hundreds or thousands of requests second, ok each command require many calls? normal redis implementation?
yes.
redis fast. benchmark on machine, or similar production machine, run redis. it included in redis itself.. (post here, i'd interested well.)
redis has lot of commands @ disposal, , info organization might allow cheaper calls, or calls-less-often. depend how lay out info model. there not "query language" sql can lots of stuff in query or combine queries single one. you meant nail redis lot, different philosophy sql (to extent).
this personal project allow see works , made better, kudos on effort. luck!
node.js redis node-redis
No comments:
Post a Comment