Insert data into REDIS (node.js + redis) -
how, can insert (store) data (node.js + redis):
var timestamp = new date().gettime(); client.hmset('room:'+room, { 'enabled' : true, timestamp : { 'g1' : 0, 'g2' : 0 } });
and how affter can increment g1 or g2 ?
p.s. when insert timestamp way, redis-cli show timestamp instead unix time
you're looking combination of hmget , hmset. according the docs:
hmget key field [field ...]
returns values associated specified fields in hash stored @ key.
for every field not exist in hash, nil value returned. because non-existing keys treated empty hashes, running hmget against non-existing key return list of nil values.
hmset key field value [field value ...]
sets specified fields respective values in hash stored @ key. this command overwrites existing fields in hash. if key not exist, new key holding hash created.
what want do, then, retrieve value has, perform operations on seem appropriate, , save on previous value.
another, possibly better solution, use hincrby. provided stick timestamp, can increment field without performing operation:
hincrby key field increment
increments number stored @ field in hash stored @ key increment. if key not exist, new key holding hash created. if field not exist value set 0 before operation performed.
the range of values supported hincrby limited 64 bit signed integers.
you need restructure hash work though, unless there way drill down g1/g2 fields (stackoverflow community, feel free edit answer or comment if know way). structure should work:
{ enabled : true, timestamp_g1 : 0, timestamp_g2 : 0 }
Comments
Post a Comment