0

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

napalias
  • 1,145
  • 1
  • 10
  • 21
  • [Isn't Unix time a timestamp?](http://en.wikipedia.org/wiki/Unix_time). You have other storage options if you don't mind the increased storage space. You could store time as a string, or even store the [date object](http://www.w3schools.com/jsref/jsref_obj_date.asp) in redis. While I haven't done that personally, it should work just fine. I don't know which option works best for you, but personally I'd store a timestamp and convert to a date object when I need to. – JoBu1324 Aug 24 '13 at 04:50

1 Answers1

2

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
}
JoBu1324
  • 7,751
  • 6
  • 44
  • 61
  • *disclaimer*: I have only dabbled in Redis a little; I'll be getting more involved in the near future. – JoBu1324 Aug 24 '13 at 04:38
  • Then how i can make timestamp_g1 to 1377519643877_g1 or g1_1377519643877 ? – napalias Aug 26 '13 at 12:26
  • I guess I don't understand your question. the property is called `timestamp_g1`, and the value of the property is an integer. Why do you need to add a prefix or suffix to the timestamp? – JoBu1324 Aug 31 '13 at 20:01
  • I rethinked, and i found another solution for what i need :) : room.g1.timestamp, count and room.g2.timestamp, count – napalias Sep 02 '13 at 20:53
  • Glad to hear you figured out your naming scheme! If my answer was relevant to your question, be sure to mark it answered and up-vote any helpful comments to reward my efforts and allow the stackoverflow community to benefit :) – JoBu1324 Sep 10 '13 at 16:10