nrk / redis-lua

A Lua client library for the redis key value storage system.
MIT License
731 stars 239 forks source link

check-and-set transactions #4

Closed slact closed 13 years ago

slact commented 13 years ago

Hey, I noticed that your transaction abstraction doesn't let you do check-and-set operations so i added a client_prototype.check_and_set function, using a coroutine or two callback blocks. So you could do something like:

redis:check_and_set("foo", function(t)
  --execute after WATCH, before MULTI
  local val = t:get("foo")
  coroutine.yield()
  t:set("foo", val .. val)
end)

I'd tried other abstractions, but a coroutine seemed to make good sense. Just in case though, you could also do:

local val
redis:check_and_set("foo", function(t)
  --execute after WATCH, before MULTI
  val = t:get("foo")
end, function(t)
  t:set("foo", val .. val)
end)

What do you think?

nrk commented 13 years ago

Thanks Leo! I'm going to review your code on the weekend when I'll have a bit more of free time.

Proper support for CAS is missing in redis-lua 2.0 because I wanted to release ASAP a stable version that was compatible with Redis 2.0 (and I was already terribly late for that), but now that Redis 2.2 is very near to hit the first RC stage I guess it's indeed a feature that should be added for a v2.0.1 of the library.

nrk commented 13 years ago

I'm closing this pull request since support for CAS transactions is almost finalized and it will soon be merged into master.

Thanks Leo!