module Redlics::Connection

Connection namespace

Public Instance Methods

create(options = {}) click to toggle source

Create a new connection pool for Redis connection.

@param options [Hash] configuration options @return [ConnectionPool] Redlics connection pool

# File lib/redlics/connection.rb, line 16
def create(options = {})
  ConnectionPool.new(pool_options(options)) do
    build_connection(options)
  end
end

Private Instance Methods

build_connection(options) click to toggle source

Build Redis connection with options.

@param options [Hash] configuration options @return [Redis] Redis connection @return [Redis::Namespace] Redis namespaced connection

# File lib/redlics/connection.rb, line 38
def build_connection(options)
  namespace = options[:namespace]
  connection = options[:redis].is_a?(Redis) ? options[:redis] : Redis.new(redis_opts(options))
  if namespace
    Redis::Namespace.new(namespace, redis: connection)
  else
    connection
  end
end
pool_options(options) click to toggle source

Set connection pool options.

@param options [Hash] configuration options @return [Hash] connection pool options

# File lib/redlics/connection.rb, line 28
def pool_options(options)
  { size:    options[:pool_size],
    timeout: options[:pool_timeout] }
end
redis_opts(options) click to toggle source

Client options provided by redis-rb @see github.com/redis/redis-rb/blob/master/lib/redis.rb

@param options [Hash] options @option options [String] :url (value of the environment variable REDIS_URL) a Redis URL, for a TCP connection: ‘redis://:@[hostname]:/[db]` (password, port and database are optional), for a unix socket connection: `unix://[path to Redis socket]`. This overrides all other options. @option options [String] :host (“127.0.0.1”) server hostname @option options [Fixnum] :port (6379) server port @option options [String] :path path to server socket (overrides host and port) @option options [Float] :timeout (5.0) timeout in seconds @option options [Float] :connect_timeout (same as timeout) timeout for initial connect in seconds @option options [String] :password Password to authenticate against server @option options [Fixnum] :db (0) Database to select after initial connect @option options [Symbol] :driver Driver to use, currently supported: `:ruby`, `:hiredis`, `:synchrony` @option options [String] :id ID for the client connection, assigns name to current connection by sending `CLIENT SETNAME` @option options [Hash, Fixnum] :tcp_keepalive Keepalive values, if Fixnum `intvl` and `probe` are calculated based on the value, if Hash `time`, `intvl` and `probes` can be specified as a Fixnum @option options [Fixnum] :reconnect_attempts Number of attempts trying to connect @option options [Boolean] :inherit_socket (false) Whether to use socket in forked process or not @option options [Array] :sentinels List of sentinels to contact @option options [Symbol] :role (:master) Role to fetch via Sentinel, either `:master` or `:slave`

# File lib/redlics/connection.rb, line 67
def redis_opts(options)
  opts = options[:redis]
  opts[:driver] ||= 'ruby'
  opts
end