Webjedi
Webjedi

Reputation: 4737

Saving and retrieving array of strings in Redis

I'm looking for some examples of getting and setting arrays of strings, and I can't seem to find one or make it work.

The strings themselves are SecureRandom.hex values. Think of them like invite codes. I want to create a pair of key/values:

1) Key=> invite:code:88bb4bdfef Value=> userid

2) Key=> userid:invite:codes Value => 88bb4bdfef,73dbfac453,etc... (one entry for each of the prior set)

I'm just getting stuck on managing the values in the second key/value pair.

UPDATE: So the challenge is that if I create an array and set it like so:

foo=Array.new
foo.push("abc")
foo.push("def")

at this point foo looks like: ["abc","def"]

So I set foo in redis, the retrieve it to bar:

$redis.set(:foo,foo)
bar=$redis.get(:foo)

Now bar looks like: "[\"abc\",\"def\"]"

Upvotes: 8

Views: 13306

Answers (1)

Chris Heald
Chris Heald

Reputation: 62688

You want lists or sets here, rather than simple keys. Here's an example using Redis' set functionality:

$redis.sadd("userid:invite:codes", ["88bb4bdfef", "73dbfac453"])
$redis.smembers("userid:invite:codes")
=> ["88bb4bdfef", "73dbfac453"]

Upvotes: 16

Related Questions