Class: Async::Redis::Client

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

Direct Known Subclasses

SentinelsClient

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



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

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.



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

def endpoint
  @endpoint
end

#protocolObject (readonly)

Returns the value of attribute protocol.



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

def protocol
  @protocol
end

Class Method Details

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

Returns if no block provided.

Yields:

  • (client, task)

    yield the client in an async task.

Returns:

  • (client)

    if no block provided.



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

def self.open(*arguments, &block)
	client = self.new(*arguments)
	
	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



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

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

#closeObject



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

def close
	@pool.close
end

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



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

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

#subscribe(*channels) ⇒ Object



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

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 Also known as: multi



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

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