ruby - Using Redis to store array of hashes -
i have started @ redis , able store array of hashes, can pop random key/value out , put in when need to.
so in ruby have this
users = [{ username: "user1", password: "password"}, { username: "user2", password: 'password'}]
so if wanted random key/value object array this
@user = users.shuffle!.pop
and put array
users.push(@user)
the idea using redis have 2 processes (ruby based app) need share pool of users @ same time. once process has finished user want put pool.
could point me in right direction please
thanks
you redis hash store user info , redis set store these hashes together.
steps:
- make redis hash hset command:
hmset userid_653 username "tom" password "gd36e3hd38d3jdj3yd3hd38"
- add hash in set called users:
sadd users userid_653
. set contains users. - get random user key set:
srandom users
. return userid_653 - get corresponding values hash using
hget userid_653 username
- if need pop key
spop userid_653
after step 3. ,sadd
again after processing in step 4.
a similar question better understanding: redis how store associative array
references:
- http://redis.io/commands/srandmember
- http://redis.io/commands/sadd
- http://redis.io/commands/spop
- http://redis.io/commands/hget
- http://redis.io/commands/hmset
ps: have no experience in ruby. suitable redis ruby api support these operations!
Comments
Post a Comment