common-pool

Object pooling with idle objects eviction check similar with Apache Common Pool.

Links:

INSTALLATION

$ gem install common-pool

EXAMPLE

require 'common_pool'    

# Extend data source object
class RandomNumberDataSource < CommonPool::PoolDataSource
  # Overwrite to return object to be stored in the pool.
  def create_object
    rand(1000)
  end

  # Overwrite to check if idle object in the pool is still valid.
  def valid?(object)
    true
  end
end

# Create a new object pool
object_pool = ObjectPool.new(RandomNumberDataSource.new)

# Borrow and return an object from the pool
object = object_pool.borrow_object
object_pool.return_object(object)

# Borrow and invalidate object
object = object_pool.borrow_object
object_pool.return_object(object)    

# Create object pool with idle objects eviction thread
object_pool = ObjectPool.new(RandomNumberDataSource.new) do |config|
  config.min_idle = 5
  config.max_idle = 10
  config.idle_check_no_per_run = 10 		# check max 10 idle objects per run
  config.idle_check_interval = 10 * 60 	# check every 10 minutes
end

# Return a hash of pool instance status variables, including active and idle objects list and 
# configuration options
object_pool.status_info