Object Pool offers easy way to build pools for objects. One common situation could be pooling database connections. This library was created for supporting pooling of GlorpDBX database connections. However, GlorpDBX related code is different package.
Objects borrowed from OPPool
have following basic lifecycle:
- Objects are first created.
- When objects are borrowed from pool they are activated.
- When objects are returned to pool they are passivated.
- When objects are no longer usable (some lifecycle operation fails) or needed it is destroyed.
To create pool for of OrderedCollections one could write:
OPBasicPool new
creator: [OrderedCollection new].
To get new collection from the pool:
pool withPooled: [:o| "Do something"].
To also clear collections when they are returned add passivator
pool passivator:[:o|o removeAll].
Or to do that before borrow add activator:
pool activator:[:o|o removeAll].
To validate objects before borrow add following. Objects that do not validate are destroyed.
pool validator:[:o|o size = 0].
One can set maximum size of pool with #maxIdleObjects. See more about pool configuration
in documentation of OPBasicPool
.
Metacello new
baseline: 'ObjectPool';
repository: 'github://pharo-ide/ObjectPool';
load
Use following snippet for stable dependency in your project baseline:
spec
baseline: 'ObjectPool'
with: [ spec repository: 'github://pharo-ide/ObjectPool:v1.0.1' ]