lib/modis.rb in modis-1.0.0 vs lib/modis.rb in modis-1.1.0

- old
+ new

@@ -1,6 +1,7 @@ require 'redis' +require 'connection_pool' require 'active_model' require 'active_support/all' require 'multi_json' require 'modis/version' @@ -13,15 +14,26 @@ require 'modis/model' module Modis @mutex = Mutex.new - def self.redis - return @redis if @redis - @mutex.synchronize { @redis = Redis.new } - @redis + class << self + attr_accessor :connection_pool, :redis_options, :connection_pool_size, + :connection_pool_timeout end - def self.redis=(redis) - @redis = redis + self.redis_options = {} + self.connection_pool_size = 5 + self.connection_pool_timeout = 5 + + def self.connection_pool + return @connection_pool if @connection_pool + @mutex.synchronize do + options = { size: connection_pool_size, timeout: connection_pool_timeout } + @connection_pool = ConnectionPool.new(options) { Redis.new(redis_options) } + end + end + + def self.with_connection + connection_pool.with { |connection| yield(connection) } end end