1
0
Fork 0
mirror of https://github.com/mperham/connection_pool synced 2023-03-27 23:22:21 -04:00

Merge pull request #19 from chatgris/README

Added some colors to README.
This commit is contained in:
Mike Perham 2012-07-05 06:48:03 -07:00
commit bdd3496d6c

View file

@ -16,30 +16,38 @@ Usage
Create a pool of objects to share amongst the fibers or threads in your Ruby application: Create a pool of objects to share amongst the fibers or threads in your Ruby application:
``` ruby
@memcached = ConnectionPool.new(:size => 5, :timeout => 5) { Dalli::Client.new } @memcached = ConnectionPool.new(:size => 5, :timeout => 5) { Dalli::Client.new }
```
Then use the pool in your application: Then use the pool in your application:
``` ruby
@memcached.with_connection do |dalli| @memcached.with_connection do |dalli|
dalli.get('some-count') dalli.get('some-count')
end end
```
You can use `ConnectionPool::Wrapper` to wrap a single global connection, making You can use `ConnectionPool::Wrapper` to wrap a single global connection, making
it easier to port your connection code over time: it easier to port your connection code over time:
``` ruby
$redis = ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) { Redis.connect } $redis = ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) { Redis.connect }
$redis.sadd('foo', 1) $redis.sadd('foo', 1)
$redis.smembers('foo') $redis.smembers('foo')
```
The Wrapper uses `method_missing` to checkout a connection, run the The Wrapper uses `method_missing` to checkout a connection, run the
requested method and then immediately check the connection back into the requested method and then immediately check the connection back into the
pool. It's **not** high-performance so you'll want to port your pool. It's **not** high-performance so you'll want to port your
performance sensitive code to use `with_connection` as soon as possible. performance sensitive code to use `with_connection` as soon as possible.
``` ruby
$redis.with_connection do |conn| $redis.with_connection do |conn|
conn.sadd('foo', 1) conn.sadd('foo', 1)
conn.smembers('foo') conn.smembers('foo')
end end
```
Once you've ported your entire system to use `with`, you can simply Once you've ported your entire system to use `with`, you can simply
remove ::Wrapper and use a simple, fast ConnectionPool. remove ::Wrapper and use a simple, fast ConnectionPool.