Saving a HASH to Redis on a rails app

后端 未结 4 1376
梦毁少年i
梦毁少年i 2021-02-02 08:36

Im just starting off with Redis with Rails so this maybe a dumb question.

I am trying to save a hash to redis server but when I retrieve it its just a string IE.

相关标签:
4条回答
  • 2021-02-02 09:02

    I should have read the redis docs more thorough.

    Answer:

    IN
    $redis.set 'data', hash.to_json
    
    OUT
    data = JSON.parse($redis.get("data"))
    
    0 讨论(0)
  • 2021-02-02 09:03

    The redis gem will remap your hash like this:

    $redis.mapped_hmset "test", { foo: "bar" }
    $redis.hgetall "test" => {"foo"=>"bar"}
    
    0 讨论(0)
  • 2021-02-02 09:06

    In order to save a hash in redis. You must pass the key as a first parameter and then the next parameters must be the keys and values on hmset method.

    $redis.hmset('user:007', :name, 'Antonio', :busy, 'maybe', :ping, 'pong')
    

    Happy Coding.

    0 讨论(0)
  • 2021-02-02 09:07

    When you use $redis.set('data', hash) you actually saving a regular Redis string, even though you use an hash variable (it might be serialized to Json string, but I'm really not sure about it).

    Try using $redis.hset('data', hash) (not by looping through the fields as you did).

    Another point: Are you sure you've deleted the previous key entirely? Did you try to hset a completely different key to eliminate the option the previous string key is still "out there"?

    0 讨论(0)
提交回复
热议问题