pooled_redis 0.1.1 → 0.2.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c1008020f33e7fb0c5ee8f942f1faa53e08bcae
4
- data.tar.gz: 6baa77f39c12fef7c17e6537fee979a52f938435
3
+ metadata.gz: 6af491df29d1c98965ba53d08ec10f9e954d6075
4
+ data.tar.gz: 5fa63b3fa1a39d9267452634eb702e773a02cf64
5
5
  SHA512:
6
- metadata.gz: 3ddf41949911ae044d4a716de9b69c849730b04310c0f7f42241328b4092415734d0f9a5e3064d7b26722d890c44ac82d5965aa0cb311b2dbfd3c783c0cb9982
7
- data.tar.gz: efbedc61537f7215cfa0643f2884c9ac4e3b20d116cff9579b4f461de9057fa6a818a29c76ed1103d2bf3f3a3dc13a55dd6966b2e6824df8edb1300fd747fb2e
6
+ metadata.gz: 28240f414268fe1bfeb0eafdc9579731c255fe64b9b2ea116efeb16d0e29836b55424106e62ec1c57e8bd01fbb85bdf2b760ca525db4bccf622239ad14586183
7
+ data.tar.gz: e9583b6fcf0aa30212149693d3e768c22cd6534d818448f6899d931db097212777182e8c96e759205abb3896b32302a1d9771548301b8bee5d67696308451c6c
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.2.0
5
+ - jruby-21mode
6
+ - rbx-2.5.2
7
+ notifications:
8
+ email: false
data/Gemfile CHANGED
@@ -1,3 +1,11 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ group :development do
6
+ gem 'redis-activesupport', '~> 4.0.0',
7
+ github: 'redis-store/redis-activesupport', ref: 'd09ae04'
8
+ gem 'pry', '~> 0.10.1'
9
+ gem 'pry-byebug', '~> 2.0.0', platforms: [:mri_20, :mri_21]
10
+ gem 'pry-coolline', '~> 0.2.5'
11
+ end
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # PooledRedis
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/pooled_redis.svg)](http://badge.fury.io/rb/pooled_redis)
4
+ [![Build Status](https://travis-ci.org/printercu/pooled_redis.svg)](https://travis-ci.org/printercu/pooled_redis)
5
+
3
6
  Simple way to access redis connections without global variables.
4
7
 
5
8
  Provides `Rails.redis_pool` & `Rails.redis` methods and configuration via `database.yml`.
@@ -31,6 +34,8 @@ production:
31
34
 
32
35
  - You can also provide `pool` & `timeout` values for ConnectionPool.
33
36
  - Use `debug: true` to set redis logger to `Rails.logger`.
37
+ - Pass `namespace` if you want to use
38
+ [redis-namespace](https://github.com/resque/redis-namespace).
34
39
  - Use `Rails.redis_pool` & `Rails.redis` method.
35
40
 
36
41
  PooledRedis uses ConnectionPool for pooling connections.
@@ -100,6 +105,11 @@ Storage.redis_pool.with { |r| r.get :some_key }
100
105
  Storage.redis.get :some_key
101
106
  ```
102
107
 
108
+ ### Advanced usage
109
+
110
+ You can return hash containing `:block` from `redis_config`. This block
111
+ will be used as a block to instantiate connection in ConnectionPool.
112
+
103
113
  # Licence
104
114
 
105
115
  MIT
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/lib/pooled_redis.rb CHANGED
@@ -1,6 +1,15 @@
1
1
  require 'pooled_redis/version'
2
2
  require 'connection_pool'
3
3
 
4
+ # If someone added hiredis to Gemfile, I hope he want to use it.
5
+ # This makes all Redis.new use hiredis.
6
+ if defined?(Hiredis)
7
+ begin
8
+ require 'redis/connection/hiredis'
9
+ rescue LoadError
10
+ end
11
+ end
12
+
4
13
  ConnectionPool.class_eval do
5
14
  # Wraps pool and proxies every method to checked out connection.
6
15
  # Similar to Wrapper, but works on existing pool.
@@ -26,31 +35,11 @@ ConnectionPool.class_eval do
26
35
  end
27
36
 
28
37
  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[:logger] = Rails.logger if config.delete(:debug)
34
- config
38
+ class << self
39
+ def extend_rails
40
+ Rails.class_eval { extend PooledRedis } if defined?(Rails)
35
41
  end
36
- end
37
42
 
38
- def redis_pool_config
39
- @redis_pool_config ||= {
40
- pool: redis_config.delete(:pool) || 5,
41
- timeout: redis_config.delete(:timeout) || 5,
42
- }
43
- end
44
-
45
- def redis_pool
46
- @redis_pool ||= ConnectionPool.new(redis_pool_config) { Redis.new(redis_config) }
47
- end
48
-
49
- def redis
50
- @redis ||= redis_pool.simple_connection
51
- end
52
-
53
- class << self
54
43
  def setup_rails_cache(app)
55
44
  # We need to use initializer to be able to access
56
45
  # Rails.configuration.database_configuration.
@@ -73,7 +62,40 @@ module PooledRedis
73
62
  end
74
63
  end
75
64
  end
65
+
66
+ # Override this method unless using Rails.
67
+ def redis_config
68
+ @redis_config ||= begin
69
+ config = ActiveRecord::Base.connection_config[:redis].with_indifferent_access
70
+ config[:logger] = Rails.logger if config.delete(:debug)
71
+ require 'redis/namespace' if config[:namespace]
72
+ config
73
+ end
74
+ end
75
+
76
+ def redis_pool_config
77
+ @redis_pool_config ||= {
78
+ pool: redis_config.delete(:pool) || 5,
79
+ timeout: redis_config.delete(:timeout) || 5,
80
+ }
81
+ end
82
+
83
+ def redis_pool
84
+ @redis_pool ||= begin
85
+ block = if redis_config[:block]
86
+ redis_config[:block]
87
+ elsif redis_config[:namespace]
88
+ -> { Redis::Namespace.new(redis_config[:namespace], redis: Redis.new(redis_config)) }
89
+ else
90
+ -> { Redis.new(redis_config) }
91
+ end
92
+ ConnectionPool.new(redis_pool_config, &block)
93
+ end
94
+ end
95
+
96
+ def redis
97
+ @redis ||= redis_pool.simple_connection
98
+ end
76
99
  end
77
100
 
78
- # Use Rails module methods to access redis client.
79
- Rails.class_eval { extend PooledRedis } if defined?(Rails)
101
+ PooledRedis.extend_rails
@@ -1,5 +1,5 @@
1
1
  module PooledRedis
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.1'
3
3
 
4
4
  def self.gem_version
5
5
  Gem::Version.new VERSION
data/pooled_redis.gemspec CHANGED
@@ -22,4 +22,10 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency 'bundler', '~> 1.7'
24
24
  spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.1.0'
26
+ spec.add_development_dependency 'rspec-its', '~> 1.2.0'
27
+ spec.add_development_dependency 'redis-namespace', '~> 1.5.1'
28
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'ruby'
29
+ spec.add_development_dependency 'hiredis', '~> 0.6.0'
30
+ end
25
31
  end
@@ -0,0 +1,55 @@
1
+ RSpec.describe PooledRedis do
2
+ context '.extend_rails' do
3
+ subject { -> { described_class.extend_rails } }
4
+ before do
5
+ expect(defined?(Rails)).to be_falsy
6
+ subject.call # ensure it doesnt fail
7
+ module Rails; end # stub
8
+ end
9
+
10
+ it 'adds methods to rails' do
11
+ subject.call
12
+ expect(Rails).to respond_to(:redis_pool, :redis)
13
+ end
14
+ end
15
+
16
+ describe '#redis_pool' do
17
+ subject { instance.redis_pool }
18
+ let(:instance) { double(redis_config: redis_config).tap { |x| x.extend described_class } }
19
+ let(:redis_config) { {} }
20
+
21
+ its(:class) { should be ConnectionPool }
22
+ its('checkout.class') { should be Redis }
23
+
24
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'ruby'
25
+ context 'on mri when hiredis is available' do
26
+ its('checkout.client.driver.name') { should eq 'Redis::Connection::Hiredis' }
27
+ end
28
+ end
29
+
30
+ context 'when config contains :namespace' do
31
+ before do
32
+ # Redis::Namespace must not use Redis.current
33
+ expect(Redis).to_not receive(:current)
34
+ expect(Redis).to receive(:new).and_call_original
35
+ end
36
+
37
+ let(:redis_config) { {namespace: 'test_ns'} }
38
+ its('checkout.class') { should be Redis::Namespace }
39
+ its('checkout.namespace') { should eq 'test_ns' }
40
+ end
41
+
42
+ context 'when config contains :block' do
43
+ let(:redis_config) { {block: -> { block_result } } }
44
+ let(:block_result) { Object.new }
45
+ its('checkout.class') { should eq block_result.class }
46
+ its('checkout') { should be block_result }
47
+ end
48
+
49
+ context 'calling multiple times' do
50
+ subject { -> { instance.redis_pool } }
51
+ let(:original_value) { subject.call }
52
+ its(:call) { should be original_value }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,27 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+ Bundler.require(:development)
4
+
5
+ require 'pooled_redis'
6
+
7
+ RSpec.configure do |config|
8
+ config.expect_with :rspec do |expectations|
9
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
10
+ end
11
+
12
+ config.mock_with :rspec do |mocks|
13
+ mocks.verify_partial_doubles = true
14
+ end
15
+
16
+ config.filter_run :focus
17
+ config.run_all_when_everything_filtered = true
18
+
19
+ config.disable_monkey_patching!
20
+
21
+ if config.files_to_run.one?
22
+ config.default_formatter = 'doc'
23
+ end
24
+
25
+ config.order = :random
26
+ Kernel.srand config.seed
27
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pooled_redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Melentiev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-26 00:00:00.000000000 Z
11
+ date: 2015-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -66,6 +66,62 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.1.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.2.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.2.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: redis-namespace
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.5.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.5.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: hiredis
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.6.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.6.0
69
125
  description: Provides `Rails.redis_pool` & `Rails.redis` methods and configuration
70
126
  via `database.yml`.
71
127
  email:
@@ -75,6 +131,8 @@ extensions: []
75
131
  extra_rdoc_files: []
76
132
  files:
77
133
  - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
78
136
  - Gemfile
79
137
  - LICENSE.txt
80
138
  - README.md
@@ -82,6 +140,8 @@ files:
82
140
  - lib/pooled_redis.rb
83
141
  - lib/pooled_redis/version.rb
84
142
  - pooled_redis.gemspec
143
+ - spec/lib/pooled_redis_spec.rb
144
+ - spec/spec_helper.rb
85
145
  homepage: https://github.com/printercu/pooled_redis
86
146
  licenses:
87
147
  - MIT
@@ -106,4 +166,6 @@ rubygems_version: 2.4.5
106
166
  signing_key:
107
167
  specification_version: 4
108
168
  summary: Simple way to access redis connections without global variables.
109
- test_files: []
169
+ test_files:
170
+ - spec/lib/pooled_redis_spec.rb
171
+ - spec/spec_helper.rb