Class: Jeredis::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/jeredis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Pool

Initialize Jedis pool

Parameters:

  • opts (Hash)

    Connection options

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :host (String) — default: "localhost"

    Hostname or IP address.

  • :port (Integer) — default: 6379

    Redis server port.

  • :timeout (Integer) — default: 5000

    Connection timeout in ms.

  • :password (String)

    Redis server password.

  • :db (Integer)

    Redis database number.

  • :min_idle (Integer)

    Min idle threads in a pool.

  • :max_idle (Integer)

    Max idle threads in a pool.

  • :max_total (Integer)

    Max total active threads.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jeredis.rb', line 23

def initialize(options = {})
  opts = options.dup
  host = opts.delete(:host) || 'localhost'
  port = opts.delete(:port) || 6379
  timeout = opts.delete(:timeout) || 5000
  password = opts.delete(:password)
  db = opts.delete(:db) || 0

  config = JedisPoolConfig.new
  config.minIdle = opts.fetch(:min_idle) if opts[:min_idle]
  config.maxIdle = opts.fetch(:max_idle) if opts[:max_idle]
  config.maxTotal = opts.fetch(:max_total) if opts[:max_total]
  config.block_when_exhausted = false
  config.max_wait_millis = timeout
  config.jmx_enabled = true

  @pool = JedisPool.new(config, host, port, timeout, password, db)
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



10
11
12
# File 'lib/jeredis.rb', line 10

def pool
  @pool
end

Instance Method Details

#pipelinedObject



49
50
51
52
53
54
55
# File 'lib/jeredis.rb', line 49

def pipelined
  with do |j|
    pipeline = j.pipelined
    response = yield pipeline
    pipeline.sync_and_return_all.to_a
  end
end

#withObject



42
43
44
45
46
47
# File 'lib/jeredis.rb', line 42

def with
  conn = @pool.resource
  yield conn
ensure
  conn.close if conn
end