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.
hash = {"field" => "value", "field2" => "value2"} $redis.set('data', hash) #So collecting the data @data = $redis.get('data')
This is obviously wrong as its returning as a string.
I have also tried looping some results and using the hset ie.
@data.each do |d| $redis.hset('data', d.field, d.value) end # errror # ERR Operation against a key holding the wrong kind of value
Not sure where to go. I have deleted the key $redis.del('data') to make sure that was not the issue.
Hope you can advise, Lee
In Redis, sets are usually implemented using hash tables.
As it is known, Redis uses the CRC16 algorithm to map keys to hash slots.
Redis doesn't support nested data structures, and specifically it doesn't support a Hash inside a Hash :) You basically have a choice between two options: either serialize the internal Hash and store it in a Hash field or use another Hash key and just keep a reference to it in a field of the outer Hash.
I should have read the redis docs more thorough.
Answer:
IN $redis.set 'data', hash.to_json OUT data = JSON.parse($redis.get("data"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With