| Class | ThreadResourcePool |
| In: |
lib/thread_resource_pool.rb
|
| Parent: | Object |
Copyright (c) 2007 FlexiGuided GmbH, Berlin
Author: Jan Behrens
Website: www.flexiguided.de/publications.flexirecord.en.html
ThreadResourcePool‘s are pools of resources to be exclusively used by each thread of an application. They are abstract classes, and can‘t be used directly. You have to create sub-classes, which implement the following three methods:
Creates a new pool with a given cache-size. If no cache-size is given, then a size of 1 is assumed.
Checks if the current thread has already an associated resource, and either yields that resource or a newly generated or cached resource to the given block. If the thread had no resource associated at the time of call, the resource will be put back to the cache or destroyed when the block ends.
Returns a new resource to use for the current thread. It is taken from the cache, or, if the cache is empty, by calling the generate_resource method, which has to be implemented by a child class of TheadResourcePool.
Puts a used resource back to the cache (or destroys it, if the cache is full). Before it is put back to the resource cache, the reset_resource method is called, which has to be implemented by a child class of ThreadResourcePool. Only if that method returns true (or any non nil/false value) the resource is used again, otherwise it is destroyed. Destruction has to be implemented as the method destroy_resource in a child class of ThreadResourcePool.