class Moneta::Pool

Creates a thread-safe pool. Stores are in the pool are transparently checked in and out in order to perform operations.

A ‘max` setting can be specified in order to limit the pool size. If `max` stores are all checked out at once, the next check-out will block until one of the other stores are checked in.

A ‘ttl` setting can be specified, giving the number of seconds to wait without any activity before shrinking the pool size back down to the min size.

A ‘timeout` setting can be specified, giving the number of seconds to wait when checking out a store, before an error is raised. When the pool has a `:max` size, a timeout is highly advisable.

@example Add ‘Moneta::Pool` to proxy stack

Moneta.build do
  use(:Pool) do
    adapter :MemcachedNative
  end
end

@example Add ‘Moneta::Pool` that contains at least 2 stores, and closes any extras after 60 seconds of inactivity

Moneta.build do
  use(:Pool, min: 2, ttl: 60) do
    adapter :Sqlite, file: 'test.db'
  end
end

@example Add ‘Moneta::Pool` with a max of 10 stores, and a timeout of 5 seconds for checkout

Moneta.build do
  use(:Pool, max: 10, timeout: 5) do
    adapter :Sqlite, file: 'test.db'
  end
end

@api public

Public Class Methods

new(options = {}, &block) click to toggle source

@param [Hash] options @option options [Integer] :min (0) The minimum pool size @option options [Integer] :max The maximum pool size. If not specified,

there is no maximum.

@option options [Numeric] :ttl The number of seconds to keep

stores above the minumum number around for without activity.  If
not specified, stores will never be removed.

@option options [Numeric] :timeout The number of seconds to wait for a

store to become available.  If not specified, will wait forever.

@yield A builder context for speciying how to construct stores

Calls superclass method
# File lib/moneta/pool.rb, line 309
def initialize(options = {}, &block)
  @id = "Moneta::Pool(#{object_id})"
  @manager = PoolManager.new(Builder.new(&block), **options)
  super(nil, options)
end

Public Instance Methods

close() click to toggle source

Closing has no effect on the pool, as stores are closed in the background by the manager after the ttl

# File lib/moneta/pool.rb, line 317
def close; end
each_key(&block) click to toggle source
# File lib/moneta/pool.rb, line 319
def each_key(&block)
  wrap(:each_key) do
    raise NotImplementedError, "each_key is not supported on this proxy" \
      unless supports? :each_key

    return enum_for(:each_key) { adapter ? adapter.each_key.size : check_out! { adapter.each_key.size } } unless block_given?

    adapter.each_key(&block)
    self
  end
end
stats() click to toggle source
# File lib/moneta/pool.rb, line 338
def stats
  @manager.stats
end
stop() click to toggle source

Tells the manager to close all stores. It will not be possible to use the store after this.

# File lib/moneta/pool.rb, line 333
def stop
  @manager.stop
  nil
end

Protected Instance Methods

adapter() click to toggle source
# File lib/moneta/pool.rb, line 344
def adapter
  Thread.current.thread_variable_get(@id)
end
adapter=(store) click to toggle source
# File lib/moneta/pool.rb, line 348
def adapter=(store)
  Thread.current.thread_variable_set(@id, store)
end
check_out!() { || ... } click to toggle source
# File lib/moneta/pool.rb, line 360
def check_out!
  store = @manager.check_out
  self.adapter = store
  yield
ensure
  self.adapter = nil
  @manager.check_in store if store
end
wrap(*args) { || ... } click to toggle source
# File lib/moneta/pool.rb, line 352
def wrap(*args, &block)
  if adapter
    yield
  else
    check_out!(&block)
  end
end