Class: Moneta::Pool

Inherits:
Wrapper show all
Defined in:
lib/moneta/pool.rb

Overview

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.

Examples:

Add ‘Moneta::Pool` to proxy stack

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

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

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

Defined Under Namespace

Classes: PoolManager, Reply, ShutdownError, TimeoutError

Instance Method Summary collapse

Methods inherited from Wrapper

#clear, #create, #delete, #each_key, #features, #fetch_values, #increment, #key?, #load, #merge!, #slice, #store, #values_at

Methods inherited from Proxy

#clear, #create, #delete, #each_key, #features, features_mask, #fetch_values, #increment, #key?, #load, #merge!, not_supports, #slice, #store, #values_at

Methods included from Defaults

#[], #[]=, #create, #decrement, #each_key, #features, #fetch, #fetch_values, included, #increment, #key?, #merge!, #slice, #supports?, #update, #values_at

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) { ... } ⇒ Pool

Returns a new instance of Pool.

Parameters:

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

Options Hash (options):

  • :min (Integer) — default: 0

    The minimum pool size

  • :max (Integer)

    The maximum pool size. If not specified, there is no maximum.

  • :ttl (Numeric)

    The number of seconds to keep stores above the minumum number around for without activity. If not specified, stores will never be removed.

  • :timeout (Numeric)

    The number of seconds to wait for a store to become available. If not specified, will wait forever.

Yields:

  • A builder context for speciying how to construct stores



291
292
293
294
295
# File 'lib/moneta/pool.rb', line 291

def initialize(options = {}, &block)
  super(nil)
  @id = "Moneta::Pool(#{object_id})"
  @manager = PoolManager.new(Builder.new(&block), **options)
end

Instance Method Details

#closeObject

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



299
# File 'lib/moneta/pool.rb', line 299

def close; end

#statsObject



308
309
310
# File 'lib/moneta/pool.rb', line 308

def stats
  @manager.stats
end

#stopObject

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



303
304
305
306
# File 'lib/moneta/pool.rb', line 303

def stop
  @manager.stop
  nil
end