Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a HASH to Redis on a rails app

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

like image 341
Lee Avatar asked Mar 22 '12 23:03

Lee


People also ask

Does Redis use hash table?

In Redis, sets are usually implemented using hash tables.

What hash function does Redis use?

As it is known, Redis uses the CRC16 algorithm to map keys to hash slots.

Does Redis support nested hash?

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.


1 Answers

I should have read the redis docs more thorough.

Answer:

IN $redis.set 'data', hash.to_json  OUT data = JSON.parse($redis.get("data")) 
like image 84
Lee Avatar answered Sep 23 '22 06:09

Lee