Class: Async::Redis::Client

Inherits:
Object
  • Object
show all
Includes:
Protocol::Redis::Methods
Defined in:
lib/async/redis/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint = Redis.local_endpoint, protocol = Protocol::RESP2, **options) ⇒ Client



45
46
47
48
49
50
# File 'lib/async/redis/client.rb', line 45

def initialize(endpoint = Redis.local_endpoint, protocol = Protocol::RESP2, **options)
  @endpoint = endpoint
  @protocol = protocol
  
  @pool = connect(**options)
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



52
53
54
# File 'lib/async/redis/client.rb', line 52

def endpoint
  @endpoint
end

#protocolObject (readonly)

Returns the value of attribute protocol.



53
54
55
# File 'lib/async/redis/client.rb', line 53

def protocol
  @protocol
end

Class Method Details

.open(*args) {|client, task| ... } ⇒ client

Returns if no block provided.

Yields:

  • (client, task)

    yield the client in an async task.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/async/redis/client.rb', line 57

def self.open(*args, &block)
  client = self.new(*args)
  
  return client unless block_given?
  
  Async do |task|
    begin
      yield client, task
    ensure
      client.close
    end
  end.wait
end

Instance Method Details

#call(*arguments) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/async/redis/client.rb', line 130

def call(*arguments)
  @pool.acquire do |connection|
    connection.write_request(arguments)
    
    connection.flush
    
    return connection.read_response
  end
end

#closeObject



71
72
73
# File 'lib/async/redis/client.rb', line 71

def close
  @pool.close
end

#multi(&block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/async/redis/client.rb', line 91

def multi(&block)
  context = Context::Multi.new(@pool)
  
  return context unless block_given?
  
  begin
    yield context
  ensure
    context.close
  end
end

#pipeline(&block) ⇒ Object Also known as: nested



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/async/redis/client.rb', line 115

def pipeline(&block)
  context = Context::Pipeline.new(@pool)

  return context unless block_given?

  begin
    yield context
  ensure
    context.close
  end
end

#publish(channel, message) ⇒ Object



75
76
77
# File 'lib/async/redis/client.rb', line 75

def publish(channel, message)
  call('PUBLISH', channel, message)
end

#subscribe(*channels) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/async/redis/client.rb', line 79

def subscribe(*channels)
  context = Context::Subscribe.new(@pool, channels)
  
  return context unless block_given?
  
  begin
    yield context
  ensure
    context.close
  end
end

#transaction(&block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/async/redis/client.rb', line 103

def transaction(&block)
  context = Context::Transaction.new(@pool)
  
  return context unless block_given?
  
  begin
    yield context
  ensure
    context.close
  end
end