Question

How, i can insert (store) data something like this (node.js + redis):

var timestamp = new Date().getTime();

client.hmset('room:'+room, {
          'enabled' : true,
           timestamp : {
              'g1' : 0,
              'g2' : 0

           }
});

and how affter that i can do increment for g1 or g2 ?

P.S. when insert timestamp this way, redis-cli show timestamp instead UNIX time

Était-ce utile?

La solution

You're looking for a combination of HMGET and HMSET. According to the docs:

HMGET key field [field ...]

Returns the values associated with the specified fields in the hash stored at key.

For every field that does not exist in the hash, a nil value is returned. Because a non-existing keys are treated as empty hashes, running HMGET against a non-existing key will return a list of nil values.

HMSET key field value [field value ...]

Sets the specified fields to their respective values in the hash stored at key. This command overwrites any existing fields in the hash. If key does not exist, a new key holding a hash is created.

What you want to do, then, is retrieve your value from the has, perform any operations on it that seem appropriate, and save over the previous value.

Another, possibly better solution, would be to use HINCRBY. Provided you stick with a timestamp, you can increment the field without performing a get operation:

HINCRBY key field increment

Increments the number stored at field in the hash stored at key by increment. If key does not exist, a new key holding a hash is created. If field does not exist the value is set to 0 before the operation is performed.

The range of values supported by HINCRBY is limited to 64 bit signed integers.

You probably will need to restructure your hash for this to work though, unless there is a way to drill down to your g1/g2 fields (stackoverflow community, feel free to edit this answer or comment it if you know a way). A structure like this should work:

{
    enabled : true,
    timestamp_g1 : 0,
    timestamp_g2 : 0
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top