class Moneta::Pool
Creates a pool of stores. Each thread gets its own store.
@example Add `Moneta::Pool` to proxy stack
Moneta.build do use(:Pool) do # Every thread gets its own instance adapter :MemcachedNative end end
@api public
Public Class Methods
new(options = {}, &block)
click to toggle source
@param [Moneta store] adapter The underlying store @param [Hash] options @option options [String] :mutex (::Mutex.new) Mutex object
Calls superclass method
# File lib/moneta/pool.rb, line 20 def initialize(options = {}, &block) super(nil) @mutex = options[:mutex] || ::Mutex.new @id = "Moneta::Pool(#{object_id})" @builder = Builder.new(&block) @pool, @all = [], [] end
Public Instance Methods
close()
click to toggle source
# File lib/moneta/pool.rb, line 28 def close @mutex.synchronize do raise '#close can only be called when no thread is using the pool' if @all.size != @pool.size @all.each(&:close) @all = @pool = nil end end
Protected Instance Methods
adapter()
click to toggle source
# File lib/moneta/pool.rb, line 38 def adapter Thread.current[@id] end
pop()
click to toggle source
# File lib/moneta/pool.rb, line 50 def pop if @mutex.synchronize { @pool.empty? } store = @builder.build.last @mutex.synchronize { @all << store } store else @mutex.synchronize { @pool.pop } end end
wrap(*args) { || ... }
click to toggle source
# File lib/moneta/pool.rb, line 42 def wrap(*args) store = Thread.current[@id] = pop yield ensure Thread.current[@id] = nil @mutex.synchronize { @pool << store } end