Created
April 25, 2017 16:06
-
-
Save IvikGH/94c217f92c467880ef1bd65bdd9bba22 to your computer and use it in GitHub Desktop.
some example to get new practices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Чтобы не быть голословным: | |
require 'redis' | |
require 'connection_pool' | |
module Shared | |
class RedisPool | |
DEFAULT_POOL_SIZE = 100.freeze # Per 1 process | |
# Closes all the connections and clears the pool | |
# | |
def shutdown | |
@pool.shutdown do |connection| | |
connection.quit | |
end | |
end | |
# Gets a connection from pool and yields a given &block | |
# | |
# @example | |
# Shared::RedisPool.new.connect { |connection| connection.get('key') } | |
# | |
def connect(&block) | |
@pool.with do |connection| | |
yield connection | |
end | |
end | |
# Delegates methods to a connection | |
# | |
# @example | |
# Shared::RedisPool.new.set('foo', 'bar') | |
# | |
def method_missing(m, *args) | |
connect do |c| | |
c.send(m, *args) | |
end | |
end | |
private | |
def initialize | |
@pool = ConnectionPool.new(size: ENV['MAX_REDIS_CONNECTIONS'].to_i | DEFAULT_POOL_SIZE, timeout: 5) do | |
ENV['REDIS_URL'] ? ::Redis.new(url: ENV['REDIS_URL']) : ::Redis.new | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment