Sha256: 025e7557c8e81c24f0865429311bba74538776f9fcf252387776ea17a2605c42

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

module Redistat
  module Connection
    
    REQUIRED_SERVER_VERSION = "1.3.10"
    
    class << self
      
      def get(ref = nil)
        ref ||= :default
        connections[references[ref]] || create
      end
      
      def add(conn, ref = nil)
        ref ||= :default
        check_redis_version(conn)
        references[ref] = conn.client.id
        connections[conn.client.id] = conn
      end
      
      def create(options = {})
        ref = options.delete(:ref) || :default
        options.reverse_merge!(default_options)
        conn = (connections[connection_id(options)] ||= connection(options))
        references[ref] = conn.client.id
        conn
      end
      
      def connections
        threaded[:connections] ||= {}
      end
      
      def references
        threaded[:references] ||= {}
      end
      
      def threaded
        Thread.current[:redistat] ||= {}
      end
      
      private
      
      def check_redis_version(conn)
        raise RedisServerIsTooOld if conn.info["redis_version"] < REQUIRED_SERVER_VERSION
        conn
      end
      
      def connection(options)
        check_redis_version(Redis.new(options))
      end
      
      def connection_id(options = {})
        options.reverse_merge!(default_options)
        "redis://#{options[:host]}:#{options[:port]}/#{options[:db]}"
      end
      
      def default_options
        {
          :host => '127.0.0.1',
          :port => 6379,
          :db => 0,
          :timeout => 5
        }
      end
      
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
redistat-0.0.7 lib/redistat/connection.rb
redistat-0.0.6 lib/redistat/connection.rb
redistat-0.0.5 lib/redistat/connection.rb