module Methods
Methods module providing Redis-specific functionality.
Definitions
def subscribe(*channels)
Subscribe to one or more channels for pub/sub messaging.
Signature
-
parameter
channels
Array(String)
The channels to subscribe to.
-
yields
{|context| ...}
If a block is given, it will be executed within the subscription context.
-
parameter
context
Context::Subscribe
The subscription context.
-
parameter
-
returns
Object
The result of the block if block given.
-
returns
Context::Subscribe
The subscription context if no block given.
Implementation
def subscribe(*channels)
context = Context::Subscribe.new(@pool, channels)
return context unless block_given?
begin
yield context
ensure
context.close
end
end
def transaction(&block)
Execute commands within a Redis transaction.
Signature
-
yields
{|context| ...}
If a block is given, it will be executed within the transaction context.
-
parameter
context
Context::Transaction
The transaction context.
-
parameter
-
returns
Object
The result of the block if block given.
-
returns
Context::Transaction
Else if no block is given, returns the transaction context.
Implementation
def transaction(&block)
context = Context::Transaction.new(@pool)
return context unless block_given?
begin
yield context
ensure
context.close
end
end
def pipeline(&block)
Execute commands in a pipeline for improved performance.
Signature
-
yields
{|context| ...}
If a block is given, it will be executed within the pipeline context.
-
parameter
context
Context::Pipeline
The pipeline context.
-
parameter
-
returns
Object
The result of the block if block given.
-
returns
Context::Pipeline
The pipeline context if no block given.
Implementation
def pipeline(&block)
context = Context::Pipeline.new(@pool)
return context unless block_given?
begin
yield context
ensure
context.close
end
end
def call(*arguments)
Execute a Redis command directly.
Signature
-
parameter
arguments
Array
The command and its arguments.
-
returns
Object
The response from the Redis server.
Implementation
def call(*arguments)
@pool.acquire do |connection|
connection.write_request(arguments)
connection.flush
return connection.read_response
end
end
def close
Close the client and all its connections.
Implementation
def close
@pool.close
end