lib/async/redis/client.rb in async-redis-0.1.0 vs lib/async/redis/client.rb in async-redis-0.2.0
- old
+ new
@@ -18,12 +18,14 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require_relative 'protocol/resp'
require_relative 'pool'
+require_relative 'context/multi'
+require_relative 'context/subscribe'
-require 'async/io/endpoint'
+require 'async/io'
module Async
module Redis
def self.local_endpoint
Async::IO::Endpoint.tcp('localhost', 6379)
@@ -32,11 +34,11 @@
class Client
def initialize(endpoint = Redis.local_endpoint, protocol = Protocol::RESP, **options)
@endpoint = endpoint
@protocol = protocol
- @connections = connect(**options)
+ @pool = connect(**options)
end
attr :endpoint
attr :protocol
@@ -51,14 +53,42 @@
client.close
end
end
def close
- @connections.close
+ @pool.close
end
+ def publish(channel, message)
+ call('PUBLISH', channel, message)
+ end
+
+ def subscribe(*channels)
+ context = Context::Subscribe.new(@pool, channels)
+
+ return context unless block_given?
+
+ begin
+ yield context
+ ensure
+ context.close
+ end
+ end
+
+ def multi(&block)
+ context = Context::Multi.new(@pool)
+
+ return context unless block_given?
+
+ begin
+ yield context
+ ensure
+ context.close
+ end
+ end
+
def call(*arguments)
- @connections.acquire do |connection|
+ @pool.acquire do |connection|
connection.write_request(arguments)
return connection.read_response
end
end