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

Keep compatibilities as possible for the transaction feature between a standalone client and a cluster client #1267

Merged
merged 1 commit into from Apr 19, 2024

Conversation

supercaracal
Copy link
Contributor

@supercaracal supercaracal commented Apr 19, 2024

There are some differences of the interface for the transaction feature between redis gem and redis-client gem. It seems that the redis-client gem is designed to be more simpler than the redis gem.

redis gem:

# @example With a block
# redis.watch("key") do
# if redis.get("key") == "some value"
# redis.multi do |multi|
# multi.set("key", "other value")
# multi.incr("counter")
# end
# else
# redis.unwatch
# end
# end
# # => ["OK", 6]

redis-client gem:
https://github.com/redis-rb/redis-client?tab=readme-ov-file#transactions

redis.multi(watch: %w[key]) do |tx|
  next unless redis.call('GET', 'key') == 'some value'

  tx.call('SET', 'key', 'other value')
  tx.call('INCR', 'counter')
end

On the redis-clustering gem side, I fixed some bugs for the transaction feature, but several compatibilities were broken.

So I tried to implement to keep the compatibility as possible by using an adapter. The adapter fills the interface gap between the redis-clustering gem and the redis-cluster-client gem. Because the cluster client implementation for the transaction feature tends to be complex for the sharding, we are hard to keep the same behaviors between the redis gem and the redis-clustering gem. But I thought it is possible up to a point.

redis.watch('{my}key') do |client|        # The client is an instance of the adapter
  if redis.get('{my}key') == 'some value' # We can't use the client passed by the block argument
    client.multi do |tx|                  # The tx is the same instance of the adapter
      tx.set('{my}key', 'other value') 
      tx.incr('{my}counter') 
    end 
  else 
    client.unwatch 
  end 
end 

@supercaracal supercaracal changed the title Keep compatibilities as possible with a standalone client for the transaction feature in a cluster client Keep compatibilities as possible for the transaction feature between a standalone client and a cluster client Apr 19, 2024
@supercaracal supercaracal marked this pull request as ready for review April 19, 2024 06:23
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

Successfully merging this pull request may close these issues.

None yet

2 participants