Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting results of pipelined commands #32

Closed
samhendley opened this issue Apr 20, 2010 · 2 comments
Closed

Getting results of pipelined commands #32

samhendley opened this issue Apr 20, 2010 · 2 comments

Comments

@samhendley
Copy link

I was using the pipeline command to do "atomic gets" on a few related items and I found that it was impossible with redis-rb today.

Naive attempt:
@r.set("k1","v1")
@r.set("k2","v2")
@r.pipelined do |pipeline|
v1 = pipeline.get("k1")
v2 = pipeline.get("k2")
end
assert_equal "v1", v1
assert_equal "v2", v2

Running this test fails beacause v1 actually equals nil since the get result hasn't been collected yet.

One way to solve this with minimal work is to return an array with the result of all pipelined operations from the pipelined block. I looked at the code and it turns out this was very easy to implement. All of the results are being collected we just needed to return them at the end of the pipeline block. Now we can write:

v1, v2 = @r.pipelined do |pipeline|
  pipeline.get("k1")
  pipeline.get("k2")
end
assert_equal "v1", v1
assert_equal "v2", v2

Another way to handle this would be with futures but that would be a bit more magical and would lead people to think they could do things like:

@r.pipelined do |pipeline|
  v1_future = pipeline.get("k1")
  pipeline.set("k2", v1_future.get)
end

This is not valid since the set command is sent to the server with the parameters already fixed. For now I think returning the values is a neat way to handle the issue. I am sending you a pull request with the change and a test case.

@djanowski
Copy link
Collaborator

That's right, it was broken. Fixed in ae35aef.

Just in case, for doing multiple atomic GETs you may be better off with MGET?

@djanowski
Copy link
Collaborator

Sam, I didn't notice you provided a commit to fix the issue. Sorry about that.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants