soveran / ohm-crystal

Ohm for Crystal

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Transactions (MULTI) support

Sija opened this issue · comments

Hello @Sija, Ohm uses Lua scripts in order to make operations atomic. In the past, before scripts were available, we relied on MULTI/EXEC. If you have something else in mind, please let me know and we can analize the use case. Thanks!

I'd need to have transaction(&block) method with rollback support in case an Exception is thrown during block execution.

My use case is as follows:

store = Store::Redis.new

store.transaction do
  store.clear # persist only when `#update` call below succeeds.
  store.update(fetch_data)
end

Ah, I'm afraid it's not possible with either MULTI/EXEC or Lua scripts.

IIRC it has been implemented by crystal-redis shard.

Gotcha! Ohm is like an ORM for Redis, while crystal-redis is a Redis client. Ohm uses a different Redis client called Resp. With Resp you can build transactions with MULTI/EXEC and you can pipeline commands. I think we can discuss your use case so I can give you an example of how to solve the problem with Resp, sounds good?

I'm aware of that, yet I couldn't find anything about MULTI/EXEC in Resp. It's great if that's already possible, I'd love to hear more! :)

Here's an example of how you can use MULTI/EXEC with Resp:

c = Resp.new("redis://localhost:6379")

c.queue("MULTI")
c.queue("ECHO", "hello")
c.queue("ECHO", "world")
c.queue("EXEC")

assert_equal ["hello", "world"], c.commit.last