AccessStack
A general-purpose object "pool" for storing general objects. It can be a connection pool, cache, or even just a factory.
Notes
AccessStack is very threadsafe. It uses an internal stack to store the objects and any interaction with that is done with a Mutex
lock in place.
AccessStack also implements most of the features present in ActiveRecord's connection pool, such as reaping, expiration, and timeout. It takes it a step farther by
Installation
Add this line to your application's Gemfile:
gem 'access_stack'
And then execute:
$ bundle
Or install it yourself as:
$ gem install access_stack
Usage
You can create a stack:
require "access_stack"
s = AccessStack.new(
:size => 10,
:timeout => 3, # how long to wait for access to the stack
:expires => 5, # how long an object lasts in the stack
:create => lambda {
PG.connect
},
:destroy => lambda { |instance|
instance.close
},
:validate => lamda { |instance|
instance.status == CONNECTION_OK
}
)
Set a stack's blocks:
s.create = lambda {
# Do something
}
Use an instance:
res = s.with { |inst| inst.exec("SELECT * FROM users;") }
# res is a PG::Result now, since that's what the block returned
Eager-load instances:
s.create_objects 5 # Create and add 5 objects to the stack
s.fill # Fill the stack to capacity
Clear out instances:
s.reap! # Validates each object using expires and validate
s.empty! # Empties the stack
s.empty? # Checks if the stack is empty
TODO
- Validate based upon methods on objects in the pool
- Reap on an interval
Contributing
- Fork it ( https://github.com/fhsjaagshs/access_stack/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request