Vitor Vezani
Vitor Vezani

Reputation: 866

Store an Array in Redis - RoR 4

I have an Item model. There is a way to store all the itens (Item.all) in a Redis database? I have tried this:

$redis = Redis::Namespace.new("suggestme", :redis => Redis.new)

$redis.set("itens", Item.all)

But when i retrieve the itens:

$redis.get("itens")

I got a String "#Item::ActiveRecord_Relation:0x007fe399d6e550"

The idea is to store the full array of itens in it

Upvotes: 3

Views: 2278

Answers (1)

pangpang
pangpang

Reputation: 8831

Redis only store string values. If you want to store other kinds of objects, you have to use marshaling. There is a built-in ruby class called Marshal, it can help you make it.

$redis.set("itens", Marshal.dump(Item.all))
Marshal.load($redis.get("itens"))

Upvotes: 4

Related Questions