Class: RediConn::RedisConnection

Inherits:
ConnectionPool
  • Object
show all
Defined in:
lib/rediconn/redis_connection.rb

Constant Summary collapse

KNOWN_REDIS_URL_ENVS =
%w[REDIS_URL REDISTOGO_URL REDISCLOUD_URL].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configured?(prefix, explicit: true) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/rediconn/redis_connection.rb', line 14

def configured?(prefix, explicit: true)
  determine_redis_provider(prefix: prefix, explicit: explicit).present?
end

.create(raw_options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rediconn/redis_connection.rb', line 22

def create(raw_options = {})
  options = raw_options.transform_keys(&:to_sym)

  if !options[:url] && (u = determine_redis_provider(prefix: raw_options[:env_prefix]))
    options[:url] = u
  end

  options[:timeout] ||= 1
  options[:size] ||= if options[:env_prefix] && (v = ENV["#{options[:env_prefix]}_REDIS_POOL_SIZE"])
    Integer(v)
  elsif ENV["RAILS_MAX_THREADS"]
    Integer(ENV["RAILS_MAX_THREADS"])
  else
    5
  end

  if options[:dedicated]
    initialize_pool(options)
  else
    url = options[:url]
    cpool = shared_pools[url] ||= initialize_pool(options)

    cpool.resize!(options[:size])

    # TODO Would be nice to respect timeouts

    cpool
  end
end

.shared_poolsObject



18
19
20
# File 'lib/rediconn/redis_connection.rb', line 18

def shared_pools
  @shared_pools ||= {}
end

Instance Method Details

#lazyObject

Mainly intended for console use



123
124
125
126
127
# File 'lib/rediconn/redis_connection.rb', line 123

def lazy
  RedisProxy.new do |&blk|
    with(&blk)
  end
end

#lazy_with(&blk) ⇒ Object



129
130
131
132
# File 'lib/rediconn/redis_connection.rb', line 129

def lazy_with(&blk)
  return lazy unless blk
  with(&blk)
end

#resize!(new_size) ⇒ Object

Very naughty, but looking at the source, should be safe



135
136
137
138
139
# File 'lib/rediconn/redis_connection.rb', line 135

def resize!(new_size)
  return unless new_size > @size
  @size = new_size
  @available.instance_variable_set(:@max, new_size)
end