pooled_redis 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 703256e707168da682b836de7be20d4e36d82cc8
4
+ data.tar.gz: 8a17b74e367e46006c91ab716095c669bbbed1e4
5
+ SHA512:
6
+ metadata.gz: 330b09edf2b189a4a54f02113f509450d244e79bd67ee2ad2de79633729a41fb40fc66d552740e632d55c2513484d2eae5deb7bf62958e1bd64490bcb85c8165
7
+ data.tar.gz: 156ed895a90febf94e3b75423b6d0a7c3cc8dfbd9b9fadfd7316c8679e53643473cdc74f8d1b1ffb80ef8460d8ac1c2589528c6dd45ad25c26d0ed24dc77bab5
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Max Melentiev
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,68 @@
1
+ # PooledRedis
2
+
3
+ Simple way to access redis connections without global variables.
4
+
5
+ Provides `Rails.redis_pool` & `Rails.redis` methods and configuration via `database.yml`.
6
+ You can add this methods to custom modules.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'pooled_redis'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ - Add `redis` section to your `database.yml` with options supported by `Redis.new`
19
+
20
+ ```yml
21
+ development:
22
+ redis:
23
+ db: 2
24
+ production:
25
+ redis:
26
+ url: 'redis://mymaster'
27
+ sentinels:
28
+ - host: host
29
+ - host: other
30
+ ```
31
+
32
+ - You can also provide `pool` & `timeout` values for ConnectionPool.
33
+ - Use `Rails.redis_pool` & `Rails.redis` method.
34
+
35
+ PooledRedis uses ConnectionPool for pooling connections.
36
+ `.redis` returns proxy object that checkouts connection for every method call.
37
+ So you may want to avoid it for bulk operations.
38
+
39
+ ### Custom modules or without rails
40
+
41
+ - Extend or include `PooledRedis` module.
42
+ - Override `redis_config` method to return valid configuration.
43
+ - Use `redis_pool` & `redis` methods.
44
+
45
+ ```ruby
46
+ class Storage
47
+ extend PooledRedis
48
+
49
+ class << self
50
+ def redis_config
51
+ read_your_yaml.symbolize_keys
52
+ end
53
+ end
54
+
55
+ # ...
56
+
57
+ def save
58
+ self.class.redis.set id, to_json
59
+ end
60
+ end
61
+
62
+ Storage.redis_pool.with { |r| r.get :some_key }
63
+ Storage.redis.get :some_key
64
+ ```
65
+
66
+ # Licence
67
+
68
+ MIT
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,7 @@
1
+ module PooledRedis
2
+ VERSION = '0.0.1'
3
+
4
+ def self.gem_version
5
+ Gem::Version.new VERSION
6
+ end
7
+ end
@@ -0,0 +1,56 @@
1
+ require 'pooled_redis/version'
2
+ require 'connection_pool'
3
+
4
+ ConnectionPool.class_eval do
5
+ # Wraps pool and proxies every method to checked out connection.
6
+ # Similar to Wrapper, but works on existing pool.
7
+ class SimpleConnection < BasicObject
8
+ def initialize(pool)
9
+ @pool = pool
10
+ end
11
+
12
+ private
13
+
14
+ def method_missing(name, *args, &block)
15
+ @pool.with { |x| x.send name, *args, &block }
16
+ end
17
+
18
+ def respond_to_missing?(name, include_all = false)
19
+ @pool.with { |x| x.respond_to?(name, include_all) }
20
+ end
21
+ end
22
+
23
+ def simple_connection
24
+ SimpleConnection.new(self)
25
+ end
26
+ end
27
+
28
+ module PooledRedis
29
+ # Override this method unless using Rails.
30
+ def redis_config
31
+ @redis_config = begin
32
+ config = ActiveRecord::Base.connection_config[:redis].with_indifferent_access
33
+ config[:driver] ||= :hiredis if defined?(Hiredis)
34
+ config[:logger] = Rails.logger if Rails.env.development?
35
+ config
36
+ end
37
+ end
38
+
39
+ def redis_pool_config
40
+ @redis_pool_config ||= {
41
+ pool: redis_config.delete(:pool) || 5,
42
+ timeout: redis_config.delete(:timeout) || 5,
43
+ }
44
+ end
45
+
46
+ def redis_pool
47
+ @redis_pool ||= ConnectionPool.new(redis_pool_config) { Redis.new(redis_config) }
48
+ end
49
+
50
+ def redis
51
+ @redis ||= redis_pool.simple_connection
52
+ end
53
+ end
54
+
55
+ # Use Rails module methods to access redis client.
56
+ Rails.class_eval { extend PooledRedis } if defined?(Rails)
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'pooled_redis/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'pooled_redis'
7
+ spec.version = PooledRedis::VERSION
8
+ spec.authors = ['Max Melentiev']
9
+ spec.email = ['melentievm@gmail.com']
10
+ spec.summary = %q{Simple way to access redis connections without global variables.}
11
+ spec.description = %q{Provides `Rails.redis_pool` & `Rails.redis` methods and configuration via `database.yml`.}
12
+ spec.homepage = 'https://github.com/printercu/pooled_redis'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'redis', '~> 3.0'
21
+ spec.add_dependency 'connection_pool', '~> 2.0'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pooled_redis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Max Melentiev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: connection_pool
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Provides `Rails.redis_pool` & `Rails.redis` methods and configuration
70
+ via `database.yml`.
71
+ email:
72
+ - melentievm@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/pooled_redis.rb
83
+ - lib/pooled_redis/version.rb
84
+ - pooled_redis.gemspec
85
+ homepage: https://github.com/printercu/pooled_redis
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.4.5
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Simple way to access redis connections without global variables.
109
+ test_files: []