mirror of
https://github.com/mperham/connection_pool
synced 2023-03-27 23:22:21 -04:00
Added some colors to README.
Signed-off-by: chatgris <jboyer@af83.com>
This commit is contained in:
parent
a425d72ac0
commit
f258f18c7b
1 changed files with 19 additions and 11 deletions
30
README.md
30
README.md
|
@ -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:
|
||||||
|
|
||||||
@memcached = ConnectionPool.new(:size => 5, :timeout => 5) { Dalli::Client.new }
|
``` ruby
|
||||||
|
@memcached = ConnectionPool.new(:size => 5, :timeout => 5) { Dalli::Client.new }
|
||||||
|
```
|
||||||
|
|
||||||
Then use the pool in your application:
|
Then use the pool in your application:
|
||||||
|
|
||||||
@memcached.with_connection do |dalli|
|
``` ruby
|
||||||
dalli.get('some-count')
|
@memcached.with_connection do |dalli|
|
||||||
end
|
dalli.get('some-count')
|
||||||
|
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:
|
||||||
|
|
||||||
$redis = ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) { Redis.connect }
|
``` ruby
|
||||||
$redis.sadd('foo', 1)
|
$redis = ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) { Redis.connect }
|
||||||
$redis.smembers('foo')
|
$redis.sadd('foo', 1)
|
||||||
|
$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.
|
||||||
|
|
||||||
$redis.with_connection do |conn|
|
``` ruby
|
||||||
conn.sadd('foo', 1)
|
$redis.with_connection do |conn|
|
||||||
conn.smembers('foo')
|
conn.sadd('foo', 1)
|
||||||
end
|
conn.smembers('foo')
|
||||||
|
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.
|
||||||
|
|
Loading…
Reference in a new issue