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, #config, #create, #delete, #features, #fetch_values, #increment, #key?, #load, #merge!, #slice, #store, #values_at

Methods inherited from Proxy

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

Methods included from Config

#config, included

Methods included from Defaults

#[], #[]=, #create, #decrement, #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



311
312
313
314
315
# File 'lib/moneta/pool.rb', line 311

def initialize(options = {}, &block)
  @id = "Moneta::Pool(#{object_id})"
  @manager = PoolManager.new(Builder.new(&block), **options)
  super(nil, 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



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

def close; end

#each_key(&block) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
# File 'lib/moneta/pool.rb', line 321

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

#statsObject



340
341
342
# File 'lib/moneta/pool.rb', line 340

def stats
  @manager.stats
end

#stopObject

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



335
336
337
338
# File 'lib/moneta/pool.rb', line 335

def stop
  @manager.stop
  nil
end