-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_config.rb
46 lines (40 loc) · 1.21 KB
/
redis_config.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# frozen_string_literal: true
# Redis Config
#
# Description: Redis configuration with sentinel configuration options.
# Author: Egon Zemmer, Phlegx Systems Technologies GmbH
module RedisConfig
class << self
def config(options = {})
if ENV['REDIS_SENTINEL_URIS'].nil?
single_config(options)
else
sentinel_config(options)
end
end
private
def single_config(options = {})
{
url: "redis://#{ENV['REDIS_URI']}/#{options.delete(:db).to_i}",
password: ENV['REDIS_PASSWORD'],
**options
}
end
def sentinel_config(options = {})
sentinels = ENV['REDIS_SENTINEL_URIS'].split(',').map do |url|
index = url.rindex(%r{:\d+(/\d*)?$})
port = index ? url[index + 1, url.length - 1].partition('/').first.strip : ENV['REDIS_SENTINEL_PORT'].to_i
host = index ? url[0, index] : url
{ host: host.strip, port: port }
end
{
sentinels: sentinels,
role: :master,
name: ENV['REDIS_SENTINEL_NAME'],
sentinel_password: ENV['REDIS_SENTINEL_PASSWORD'],
password: ENV['REDIS_PASSWORD'],
**options
}
end
end
end