Redis pipelined order of execution

2019-05-26 18:56发布

I am using rub redis gem. Wondering if I do for example:

redis.pipelined do
    REDIS.del("users:#{current_user_id}:i-unread")
    REDIS.lpush("users:#{current_user_id}:i-read", items)
    REDIS.ltrim("users:#{current_user_id}:i-read", 0, Interaction::MAX_INTERACTIONS)
end

is this order of execution guaranteed?

标签: ruby redis
2条回答
Melony?
2楼-- · 2019-05-26 19:14

of course the order is guaranteed, otherwise pipelining would be useless. you can always look at the code. for example, this test clearly assumes that the commands are executed sequentially: https://github.com/redis/redis-rb/blob/master/test/pipelining_commands_test.rb#L32

def test_bulk_and_multi_bulk_commands_mixed
  r.pipelined do
    r.lpush "foo", "s1"
    r.lpush "foo", "s2"
    r.mset("baz", "s3", "qux", "s4")
  end

  assert_equal 2, r.llen("foo")
  assert_equal "s2", r.lpop("foo")
  assert_equal "s1", r.lpop("foo")
  assert_equal "s3", r.get("baz")
  assert_equal "s4", r.get("qux")
end
查看更多
虎瘦雄心在
3楼-- · 2019-05-26 19:15

Yes, it is guaranteed. Generally, redis pipelining is nothing more complicated than queuing server responses in memory, so it does not change the order of queries.

查看更多
登录 后发表回答