redis_connector 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format Fuubar --order rand
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_connector.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sergio Tulentsev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RedisConnector
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'redis_connector'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install redis_connector
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ class RedisConnector
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,121 @@
1
+ require "redis_connector/version"
2
+
3
+ class RedisConnector
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.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
82
+
83
+ args = {zkservers: zk, znode_path: znode}
84
+ args.merge!(namespace: params[:namespace]) if params[:namespace]
85
+ redis = RedisFailover::Client.new args
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'
101
+
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,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_connector/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "redis_connector"
8
+ gem.version = RedisConnector::VERSION
9
+ gem.authors = ["Sergio Tulentsev"]
10
+ gem.email = ["sergei.tulentsev@gmail.com"]
11
+ gem.description = %q{handles multiple redis connections}
12
+ gem.summary = %q{handles multiple redis connections}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'redis'
21
+ gem.add_dependency 'connection_pool'
22
+
23
+
24
+ gem.add_development_dependency 'rspec'
25
+ gem.add_development_dependency 'turn'
26
+ gem.add_development_dependency 'fuubar'
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe RedisConnector do
4
+ it 'passes this test' do
5
+ true
6
+ end
7
+
8
+ it 'fails this test' do
9
+ raise 'oops'
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ # figure out where we are being loaded from
2
+ if $LOADED_FEATURES.grep(/spec\/spec_helper\.rb/).any?
3
+ begin
4
+ raise "foo"
5
+ rescue => e
6
+ puts <<-MSG
7
+ ===================================================
8
+ It looks like spec_helper.rb has been loaded
9
+ multiple times. Normalize the require to:
10
+
11
+ require "spec/spec_helper"
12
+
13
+ Things like File.join and File.expand_path will
14
+ cause it to be loaded multiple times.
15
+
16
+ Loaded this time from:
17
+
18
+ #{e.backtrace.join("\n ")}
19
+ ===================================================
20
+ MSG
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_connector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sergio Tulentsev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: connection_pool
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: turn
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: fuubar
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: handles multiple redis connections
95
+ email:
96
+ - sergei.tulentsev@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - lib/redis_connector.rb
108
+ - lib/redis_connector/version.rb
109
+ - redis_connector.gemspec
110
+ - spec/lib/redis_connector_spec.rb
111
+ - spec/spec_helper.rb
112
+ homepage: ''
113
+ licenses: []
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 1.8.24
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: handles multiple redis connections
136
+ test_files:
137
+ - spec/lib/redis_connector_spec.rb
138
+ - spec/spec_helper.rb
139
+ has_rdoc: