Class: Redic::Pool

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

Constant Summary collapse

VERSION =
"1.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Pool

Returns a new instance of Pool.



10
11
12
13
14
15
# File 'lib/redic/pool.rb', line 10

def initialize(url, options = {})
  @url = url
  @pool = ConnectionPool.new(size: options.fetch(:size, 10)) { Redic.new(url) }

  @id = "redic-pool-#{object_id}"
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



8
9
10
# File 'lib/redic/pool.rb', line 8

def pool
  @pool
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/redic/pool.rb', line 7

def url
  @url
end

Instance Method Details

#call(*args) ⇒ Object



17
18
19
20
21
# File 'lib/redic/pool.rb', line 17

def call(*args)
  @pool.with do |client|
    client.call(*args)
  end
end

#commitObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/redic/pool.rb', line 28

def commit
  @pool.with do |client|
    Thread.current[@id].each do |args|
      client.queue(*args)
    end

    result = client.commit

    Thread.current[@id].clear

    result
  end
end

#queue(*args) ⇒ Object



23
24
25
26
# File 'lib/redic/pool.rb', line 23

def queue(*args)
  Thread.current[@id] || (Thread.current[@id] = [])
  Thread.current[@id] << args
end