Skip to content

Commit

Permalink
Rework & simplify contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 17, 2019
1 parent 3d56c93 commit ca75d56
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 63 deletions.
8 changes: 4 additions & 4 deletions lib/async/redis/client.rb
Expand Up @@ -19,9 +19,9 @@
# THE SOFTWARE.

require_relative 'pool'
require_relative 'context/multi'
require_relative 'context/subscribe'
require_relative 'context/pipeline'
require_relative 'context/transaction'
require_relative 'context/subscribe'

require_relative 'protocol/resp2'

Expand Down Expand Up @@ -100,8 +100,8 @@ def multi(&block)
end
end

def nested(&block)
context = Context::Nested.new(@pool)
def transaction(&block)
context = Context::Transaction.new(@pool)

return context unless block_given?

Expand Down
32 changes: 0 additions & 32 deletions lib/async/redis/context/nested.rb

This file was deleted.

4 changes: 3 additions & 1 deletion lib/async/redis/context/pipeline.rb
Expand Up @@ -25,7 +25,9 @@ module Async
module Redis
module Context
# Send multiple commands without waiting for the response, instead of sending them one by one.
class Pipeline < Nested
class Pipeline < Generic
include ::Protocol::Redis::Methods

class Sync
include ::Protocol::Redis::Methods

Expand Down
4 changes: 2 additions & 2 deletions lib/async/redis/context/subscribe.rb
Expand Up @@ -19,12 +19,12 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require_relative 'nested'
require_relative 'generic'

module Async
module Redis
module Context
class Subscribe < Nested
class Subscribe < Generic
def initialize(pool, channels)
super(pool)

Expand Down
Expand Up @@ -19,26 +19,30 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require_relative 'nested'
require_relative 'pipeline'

module Async
module Redis
module Context
class Multi < Nested
class Transaction < Pipeline
def initialize(pool, *args)
super(pool)

@connection.write_request(['MULTI'])
@connection.flush
@connection.read_response
end

def multi
sync.call('MULTI')
end

def watch(*keys)
sync.call('WATCH', *keys)
end

def execute
call 'EXEC'
sync.call('EXEC')
end

def discard
call 'DISCARD'
sync.call('DISCARD')
end
end
end
Expand Down
Expand Up @@ -20,20 +20,22 @@

require 'async/redis/client'

RSpec.describe Async::Redis::Context::Multi, timeout: 5 do
RSpec.describe Async::Redis::Context::Transaction, timeout: 5 do
include_context Async::RSpec::Reactor

let(:endpoint) {Async::Redis.local_endpoint}
let(:client) {Async::Redis::Client.new(endpoint)}

let (:multi_key_base) {"async-redis:test:multi"}

it "can atomically execute commands in a multi" do
it "can atomically execute commands" do
response = nil

client.multi do |context|
client.transaction do |context|
context.multi

(0..5).each do |id|
response = context.set "#{multi_key_base}:#{id}", "multi-test 6"
response = context.sync.set "#{multi_key_base}:#{id}", "multi-test 6"
expect(response).to be == "QUEUED"
end

Expand Down
12 changes: 0 additions & 12 deletions spec/async/redis/performance_spec.rb
Expand Up @@ -45,18 +45,6 @@
end
end

benchmark.report("async-redis (nested)") do |times|
key = keys.sample
value = times.to_s

async_client.nested do |nested|
i = 0; while i < times; i += 1
nested.set(key, value)
expect(nested.get(key)).to be == value
end
end
end

benchmark.report("async-redis (pipeline)") do |times|
key = keys.sample
value = times.to_s
Expand Down

0 comments on commit ca75d56

Please sign in to comment.