LoginSignup
3
3

More than 5 years have passed since last update.

Redisデータアクセスコードサンプル for Ruby

Last updated at Posted at 2016-08-16

RubyでRedisにつないでデータのやり取りをするためのサンプルコード。

よく忘れるのでメモとしてまとめておく。

require 'redis'

redis = Redis.new(:host => "localhost", :port => 6379)


#FLUSH
redis.flushdb

#SET
redis.set("test", 1)
redis.set("KEY", "value")

#GET
p redis.get("test")
p redis.get("KEY")
p redis.get("aaa")

#HMSET
redis.hmset("hash", "k1", 1, "k2", "val")

#HMGET
p redis.hmget("hash", "k1", "k2")

#PUSH, POP, RANGE
redis.rpush("data", 1 )
redis.rpush("data", 2)
redis.rpush("data", 3)
redis.rpush("data", 4)
redis.rpush("data", 5)
redis.lpush("data", 0)

p redis.lrange("data", 0, -1)
p redis.rpop("data")
p redis.lpop("data")

#SET
redis.sadd("set", "a")
redis.sadd("set", "b")
redis.sadd("set", "a")

redis.sadd("set2", "a")

p redis.smembers("set")
p redis.sinter("set", "set2")
p redis.sdiff("set", "set2")

#ZADD
redis.zadd("score", [[100, "aaaaa"], [10, "bbbbb"], [200, "ccccc"]])
p redis.zrange("score", 0, -1, with_scores:   true)
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3