redis_connector 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +8 -0
- data/lib/redis_connector.rb +7 -119
- data/lib/redis_connector/connector.rb +121 -0
- data/lib/redis_connector/util.rb +18 -0
- data/lib/redis_connector/version.rb +1 -1
- data/spec/lib/redis_connector_spec.rb +45 -7
- data/spec/spec_helper.rb +5 -0
- data/spec/support/config/example01.yml +13 -0
- data/spec/support/config/example01_multiple_environments.yml +14 -0
- metadata +7 -1
data/Rakefile
CHANGED
data/lib/redis_connector.rb
CHANGED
@@ -1,121 +1,9 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
p = pools[name]
|
6
|
-
raise "No session configuration for #{name}" unless p
|
7
|
-
|
8
|
-
p.with(&block)
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.session_names
|
12
|
-
pools.keys
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.pools
|
16
|
-
@pools || {}
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.load_config config = nil
|
20
|
-
if config_file.file?
|
21
|
-
::RedisConnector.load!(config_file)
|
22
|
-
else
|
23
|
-
raise "File not found: config/redis_connector.yml"
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.load! filename
|
29
|
-
conf = YAML::load_file filename
|
30
|
-
unless conf && conf[current_env]
|
31
|
-
raise "RedisConnector: no configuration for :#{current_env} environment."
|
32
|
-
end
|
33
|
-
|
34
|
-
envconf = conf[current_env].symbolize_keys
|
35
|
-
|
36
|
-
unless envconf[:sessions]
|
37
|
-
raise "Missing :sessions section in redis_connector.yml for :#{current_env} environement"
|
38
|
-
end
|
39
|
-
|
40
|
-
@connection_configs = envconf[:sessions].each_with_object({}) do |(name, prm), memo|
|
41
|
-
memo[name.to_sym] = insert_default_values(prm)
|
42
|
-
end
|
43
|
-
|
44
|
-
establish_connections!
|
45
|
-
end
|
46
|
-
|
47
|
-
def self.establish_connections!
|
48
|
-
@pools = {}
|
49
|
-
@connection_configs.each do |name, params|
|
50
|
-
params[:znode_alias] ||= name
|
51
|
-
|
52
|
-
@pools[name] = ConnectionPool.new(size: params[:pool_size], timeout: params[:timeout]) do
|
53
|
-
case params[:driver]
|
54
|
-
when 'redis'
|
55
|
-
init_vanilla_redis(params)
|
56
|
-
when 'redis_failover'
|
57
|
-
init_failover_redis(params)
|
58
|
-
else
|
59
|
-
raise "Unrecognized redis driver: #{driver.inspect}"
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def self.close_connections!
|
66
|
-
end
|
67
|
-
|
68
|
-
def self.init_vanilla_redis params
|
69
|
-
redis = Redis.new host: params[:host], port: params[:port]
|
70
|
-
redis = Redis::Namespace.new(params[:namespace], redis: redis) if params[:namespace]
|
71
|
-
redis
|
72
|
-
end
|
73
|
-
|
74
|
-
def self.init_failover_redis params
|
75
|
-
if defined? RedisFailover::Client
|
76
|
-
zk = params.fetch(:zkservers)
|
77
|
-
znode = "/rf_#{name}"
|
78
|
-
|
79
|
-
if za = params[:znode_alias]
|
80
|
-
znode = "/rf_#{za}"
|
81
|
-
end
|
1
|
+
require 'yaml'
|
2
|
+
require 'redis'
|
3
|
+
require 'redis-namespace'
|
4
|
+
require 'connection_pool'
|
82
5
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
redis
|
87
|
-
else
|
88
|
-
raise "RedisFailover::Client is not found. Add it to Gemfile and run `bundle install`"
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def self.insert_default_values prm
|
93
|
-
res = prm.symbolize_keys
|
94
|
-
|
95
|
-
res[:host] ||= 'localhost'
|
96
|
-
res[:port] ||= 6379
|
97
|
-
|
98
|
-
res[:pool_size] ||= 1
|
99
|
-
res[:timeout] ||= 5
|
100
|
-
res[:driver] ||= 'redis'
|
6
|
+
require 'redis_connector/util'
|
7
|
+
require "redis_connector/version"
|
8
|
+
require 'redis_connector/connector'
|
101
9
|
|
102
|
-
res
|
103
|
-
end
|
104
|
-
|
105
|
-
private
|
106
|
-
def self.current_env
|
107
|
-
if defined? Rails
|
108
|
-
Rails.env
|
109
|
-
else
|
110
|
-
ENV['RAILS_ENV'] || ENV['CURRENT_ENV'] || 'development'
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def self.config_file
|
115
|
-
if defined? Rails
|
116
|
-
Rails.root.join("config", "redis_connector.yml")
|
117
|
-
else
|
118
|
-
'config/redis_connector.yml'
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
class RedisConnector
|
2
|
+
extend Util
|
3
|
+
|
4
|
+
def self.with_session name = :default, &block
|
5
|
+
p = pools[name]
|
6
|
+
raise "No session configuration for #{name}" unless p
|
7
|
+
|
8
|
+
p.with(&block)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.session_names
|
12
|
+
pools.keys
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.pools
|
16
|
+
@pools || {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configure config = nil
|
20
|
+
|
21
|
+
envconf = if config && config.is_a?(Hash)
|
22
|
+
config = stringify_keys(config)
|
23
|
+
config[current_env] || config
|
24
|
+
else
|
25
|
+
config ||= config_file
|
26
|
+
|
27
|
+
conf = YAML::load_file config
|
28
|
+
|
29
|
+
envconf = conf[current_env] || conf
|
30
|
+
end
|
31
|
+
|
32
|
+
envconf = symbolize_keys(envconf)
|
33
|
+
|
34
|
+
unless envconf[:sessions]
|
35
|
+
raise "Missing :sessions section in redis_connector.yml for :#{current_env} environement"
|
36
|
+
end
|
37
|
+
|
38
|
+
@connection_configs = envconf[:sessions].each_with_object({}) do |(name, prm), memo|
|
39
|
+
memo[name.to_sym] = insert_default_values(prm)
|
40
|
+
end
|
41
|
+
|
42
|
+
establish_connections!
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.establish_connections!
|
46
|
+
@pools = {}
|
47
|
+
@connection_configs.each do |name, params|
|
48
|
+
params[:znode_alias] ||= name
|
49
|
+
|
50
|
+
@pools[name] = ConnectionPool.new(size: params[:pool_size], timeout: params[:timeout]) do
|
51
|
+
case params[:driver]
|
52
|
+
when 'redis'
|
53
|
+
init_vanilla_redis(params)
|
54
|
+
when 'redis_failover'
|
55
|
+
init_failover_redis(params)
|
56
|
+
else
|
57
|
+
raise "Unrecognized redis driver: #{driver.inspect}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.close_connections!
|
64
|
+
# we should call #quit on all connections
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.init_vanilla_redis params
|
68
|
+
redis = Redis.new host: params[:host], port: params[:port]
|
69
|
+
redis = Redis::Namespace.new(params[:namespace], redis: redis) if params[:namespace]
|
70
|
+
redis
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.init_failover_redis params
|
74
|
+
if defined? RedisFailover::Client
|
75
|
+
zk = params.fetch(:zkservers)
|
76
|
+
znode = "/rf_#{name}"
|
77
|
+
|
78
|
+
if za = params[:znode_alias]
|
79
|
+
znode = "/rf_#{za}"
|
80
|
+
end
|
81
|
+
|
82
|
+
args = {zkservers: zk, znode_path: znode}
|
83
|
+
args.merge!(namespace: params[:namespace]) if params[:namespace]
|
84
|
+
redis = RedisFailover::Client.new args
|
85
|
+
redis
|
86
|
+
else
|
87
|
+
raise "RedisFailover::Client is not found. Add it to Gemfile and run `bundle install`"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.insert_default_values prm
|
92
|
+
res = symbolize_keys(prm)
|
93
|
+
|
94
|
+
res[:host] ||= 'localhost'
|
95
|
+
res[:port] ||= 6379
|
96
|
+
|
97
|
+
res[:pool_size] ||= 1
|
98
|
+
res[:timeout] ||= 5
|
99
|
+
res[:driver] ||= 'redis'
|
100
|
+
|
101
|
+
res
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
def self.current_env
|
106
|
+
env = if defined? Rails
|
107
|
+
Rails.env
|
108
|
+
else
|
109
|
+
ENV['RAILS_ENV'] || ENV['CURRENT_ENV'] || 'development'
|
110
|
+
end
|
111
|
+
env.to_s
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.config_file
|
115
|
+
if defined? Rails
|
116
|
+
Rails.root.join("config", "redis_connector.yml")
|
117
|
+
else
|
118
|
+
'config/redis_connector.yml'
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Util
|
2
|
+
# Symbolizes the keys of the specified hash.
|
3
|
+
#
|
4
|
+
# @param [Hash] hash a hash for which keys should be symbolized
|
5
|
+
# @return [Hash] a new hash with symbolized keys
|
6
|
+
def symbolize_keys(hash)
|
7
|
+
Hash[hash.map { |k, v| [k.to_sym, v] }]
|
8
|
+
end
|
9
|
+
|
10
|
+
# Stringifies the keys of the specified hash.
|
11
|
+
#
|
12
|
+
# @param [Hash] hash a hash for which keys should be stringified
|
13
|
+
# @return [Hash] a new hash with stringified keys
|
14
|
+
def stringify_keys(hash)
|
15
|
+
Hash[hash.map { |k, v| [k.to_s, v] }]
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -1,11 +1,49 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe RedisConnector do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
describe '#configure' do
|
5
|
+
it 'accepts plain hash' do
|
6
|
+
conf = {
|
7
|
+
sessions: {
|
8
|
+
cache: {
|
9
|
+
host: 'localhost'
|
10
|
+
},
|
11
|
+
resque: {
|
12
|
+
host: 'localhost'
|
13
|
+
}
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
RedisConnector.configure conf
|
18
|
+
RedisConnector.session_names.should =~ [:resque, :cache]
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'accepts hash with environment sections' do
|
22
|
+
conf = {
|
23
|
+
test: {
|
24
|
+
sessions: {
|
25
|
+
cache: {
|
26
|
+
host: 'localhost'
|
27
|
+
},
|
28
|
+
resque: {
|
29
|
+
host: 'localhost'
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
RedisConnector.configure conf
|
36
|
+
RedisConnector.session_names.should =~ [:resque, :cache]
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'accepts a filename with simple config' do
|
40
|
+
RedisConnector.configure 'spec/support/config/example01.yml'
|
41
|
+
RedisConnector.session_names.should =~ [:resque, :cache, :default]
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'accepts a filename with environment sections' do
|
45
|
+
RedisConnector.configure 'spec/support/config/example01_multiple_environments.yml'
|
46
|
+
RedisConnector.session_names.should =~ [:resque, :default, :cache]
|
47
|
+
end
|
10
48
|
end
|
11
|
-
end
|
49
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_connector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -105,10 +105,14 @@ files:
|
|
105
105
|
- README.md
|
106
106
|
- Rakefile
|
107
107
|
- lib/redis_connector.rb
|
108
|
+
- lib/redis_connector/connector.rb
|
109
|
+
- lib/redis_connector/util.rb
|
108
110
|
- lib/redis_connector/version.rb
|
109
111
|
- redis_connector.gemspec
|
110
112
|
- spec/lib/redis_connector_spec.rb
|
111
113
|
- spec/spec_helper.rb
|
114
|
+
- spec/support/config/example01.yml
|
115
|
+
- spec/support/config/example01_multiple_environments.yml
|
112
116
|
homepage: ''
|
113
117
|
licenses: []
|
114
118
|
post_install_message:
|
@@ -136,4 +140,6 @@ summary: handles multiple redis connections
|
|
136
140
|
test_files:
|
137
141
|
- spec/lib/redis_connector_spec.rb
|
138
142
|
- spec/spec_helper.rb
|
143
|
+
- spec/support/config/example01.yml
|
144
|
+
- spec/support/config/example01_multiple_environments.yml
|
139
145
|
has_rdoc:
|