Class: Async::Redis::Client

Inherits:
Object
  • Object
show all
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::RESP, **options) ⇒ Client

Returns a new instance of Client.



35
36
37
38
39
40
# File 'lib/async/redis/client.rb', line 35

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

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



42
43
44
# File 'lib/async/redis/client.rb', line 42

def endpoint
  @endpoint
end

#protocolObject (readonly)

Returns the value of attribute protocol.



43
44
45
# File 'lib/async/redis/client.rb', line 43

def protocol
  @protocol
end

Class Method Details

.open(*args, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/async/redis/client.rb', line 45

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

Instance Method Details

#call(*arguments) ⇒ Object



89
90
91
92
93
94
# File 'lib/async/redis/client.rb', line 89

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

#closeObject



57
58
59
# File 'lib/async/redis/client.rb', line 57

def close
  @pool.close
end

#multi(&block) ⇒ Object



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

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

#publish(channel, message) ⇒ Object



61
62
63
# File 'lib/async/redis/client.rb', line 61

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

#subscribe(*channels) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/async/redis/client.rb', line 65

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