modis 4.2.0 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f75b1f2591f788a511391d33f34ee1700d4c5aac164d46b7c41c0022afe1c6c8
4
- data.tar.gz: 36ae08b35f2cc34166322ff2933d42717fd826779501eb135e815fccfaddecd5
3
+ metadata.gz: 9a9d3d05277e2ddf26b7c14547b3e03f37cb6fbb771ba9b2a6b61acce6485bd9
4
+ data.tar.gz: 031d2f34a15d5f077b5b01c2e4286b73285ed2171f4029577c2d812a22f6da6d
5
5
  SHA512:
6
- metadata.gz: ff27792407942c2b3618ae6751e0397ba7c510d7852a758eb9e21219c8d111ebf3b0f28c2be3db40ec846de20bda4a9dc53d65051e1ad15d5ccfe6b6f3588118
7
- data.tar.gz: 9ec6aaf991906929d9101ebf502bfd0f4d669af5c70df4b04b835ea714cc6602497a291aeb03f84ca436785aa0b0601a0404f1c44e79e147fe457f372f8527a7
6
+ metadata.gz: 9b76390c567d8ba5495c226cb7425372797fe9b3d5e2d2a07a8f891bed7e5aa4f3c66d84f54c6cac7814f07f156e958687a62da8179c4d1922da2efb065412c1
7
+ data.tar.gz: dba384ff8ad647615016ed340c21e72088d45b1186b91e96f28c579b73ff7d45cd2616b10c70d42fcb17e1f4cfe4889074c0eb8a6ab41b0e92082d83d663c026
@@ -26,6 +26,7 @@ jobs:
26
26
  - 6379:6379
27
27
 
28
28
  strategy:
29
+ fail-fast: false
29
30
  matrix:
30
31
  gemfile: ['rails_5.2', 'rails_6.0', 'rails_6.1', 'rails_7.0']
31
32
 
@@ -55,7 +56,7 @@ jobs:
55
56
  BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
56
57
 
57
58
  steps:
58
- - uses: actions/checkout@v2
59
+ - uses: actions/checkout@v4
59
60
 
60
61
  - name: Set up Ruby
61
62
  uses: ruby/setup-ruby@v1
data/CHANGELOG.md CHANGED
@@ -1,8 +1,17 @@
1
1
  # Changelog
2
2
 
3
- ## [v4.1.0](https://github.com/rpush/modis/tree/v4.1.0) (2023-08-01)
3
+ ## [v4.1.0](https://github.com/rpush/modis/tree/v4.1.0) (2024-07-31)
4
4
 
5
- [Full Changelog](https://github.com/rpush/modis/compare/v4.1.0...v4.1.0)
5
+ [Full Changelog](https://github.com/rpush/modis/compare/v4.2.0...v4.1.0)
6
+
7
+ **Merged pull requests:**
8
+
9
+ - Add support for multiple redis connections [\#49](https://github.com/rpush/modis/pull/49) ([SxDx](https://github.com/SxDx))
10
+ - Fix deprecation warning [\#48](https://github.com/rpush/modis/pull/48) ([SxDx](https://github.com/SxDx))
11
+
12
+ ## [v4.2.0](https://github.com/rpush/modis/tree/v4.2.0) (2023-08-01)
13
+
14
+ [Full Changelog](https://github.com/rpush/modis/compare/v4.1.0...v4.2.0)
6
15
 
7
16
  **Merged pull requests:**
8
17
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- modis (4.2.0)
4
+ modis (4.3.0)
5
5
  activemodel (>= 5.2)
6
6
  activesupport (>= 5.2)
7
7
  connection_pool (>= 2)
@@ -48,8 +48,8 @@ GEM
48
48
  parallel
49
49
  codeclimate-test-reporter (1.0.7)
50
50
  simplecov
51
- concurrent-ruby (1.2.0)
52
- connection_pool (2.3.0)
51
+ concurrent-ruby (1.2.2)
52
+ connection_pool (2.4.1)
53
53
  console (1.16.2)
54
54
  fiber-local
55
55
  diff-lcs (1.4.4)
@@ -71,12 +71,12 @@ GEM
71
71
  rainbow (>= 2.2.1)
72
72
  rake (>= 10.0)
73
73
  hiredis (0.6.3)
74
- i18n (1.12.0)
74
+ i18n (1.14.1)
75
75
  concurrent-ruby (~> 1.0)
76
76
  io-event (1.1.6)
77
77
  jaro_winkler (1.5.4)
78
- minitest (5.17.0)
79
- msgpack (1.6.0)
78
+ minitest (5.19.0)
79
+ msgpack (1.7.2)
80
80
  multi_json (1.15.0)
81
81
  octokit (4.25.1)
82
82
  faraday (>= 1, < 3)
@@ -94,9 +94,9 @@ GEM
94
94
  public_suffix (5.0.1)
95
95
  rainbow (3.0.0)
96
96
  rake (13.0.1)
97
- redis (5.0.6)
98
- redis-client (>= 0.9.0)
99
- redis-client (0.12.1)
97
+ redis (5.2.0)
98
+ redis-client (>= 0.22.0)
99
+ redis-client (0.22.2)
100
100
  connection_pool
101
101
  rexml (3.2.5)
102
102
  rspec (3.9.0)
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Modis
4
+ def self.deprecator
5
+ @deprecator ||= ActiveSupport::Deprecation.new('5.0', 'modis')
6
+ end
7
+ end
data/lib/modis/finder.rb CHANGED
@@ -18,7 +18,7 @@ module Modis
18
18
  "because you disabled all index. See :enable_all_index for more."
19
19
  end
20
20
 
21
- records = Modis.with_connection do |redis|
21
+ records = Modis.with_connection(modis_connection) do |redis|
22
22
  ids = redis.smembers(key_for(:all))
23
23
  redis.pipelined do |pipeline|
24
24
  ids.map { |id| record_for(pipeline, id) }
@@ -41,7 +41,7 @@ module Modis
41
41
  def find_all(ids)
42
42
  raise RecordNotFound, "Couldn't find #{name} without an ID" if ids.empty?
43
43
 
44
- records = Modis.with_connection do |redis|
44
+ records = Modis.with_connection(modis_connection) do |redis|
45
45
  if ids.count == 1
46
46
  ids.map { |id| record_for(redis, id) }
47
47
  else
data/lib/modis/index.rb CHANGED
@@ -36,7 +36,7 @@ module Modis
36
36
  end
37
37
 
38
38
  def index_for(attribute, value)
39
- Modis.with_connection do |redis|
39
+ Modis.with_connection(modis_connection) do |redis|
40
40
  key = index_key(attribute, value)
41
41
  redis.smembers(key).map(&:to_i)
42
42
  end
data/lib/modis/model.rb CHANGED
@@ -21,6 +21,8 @@ module Modis
21
21
  include Modis::Index
22
22
 
23
23
  base.extend(ClassMethods)
24
+ base.class_attribute :modis_connection
25
+ base.modis_connection = :default
24
26
  end
25
27
  end
26
28
 
@@ -150,7 +150,7 @@ module Modis
150
150
  end
151
151
 
152
152
  def reload
153
- new_attributes = Modis.with_connection { |redis| self.class.attributes_for(redis, id) }
153
+ new_attributes = Modis.with_connection(modis_connection) { |redis| self.class.attributes_for(redis, id) }
154
154
  initialize(new_attributes)
155
155
  self
156
156
  end
@@ -166,7 +166,7 @@ module Modis
166
166
  end
167
167
 
168
168
  alias update_attributes update
169
- deprecate update_attributes: 'please, use update instead'
169
+ deprecate update_attributes: 'please, use update instead', deprecator: Modis.deprecator
170
170
 
171
171
  def update!(attrs)
172
172
  assign_attributes(attrs)
@@ -174,7 +174,7 @@ module Modis
174
174
  end
175
175
 
176
176
  alias update_attributes! update!
177
- deprecate update_attributes!: 'please, use update! instead'
177
+ deprecate update_attributes!: 'please, use update! instead', deprecator: Modis.deprecator
178
178
 
179
179
  private
180
180
 
@@ -253,7 +253,7 @@ module Modis
253
253
 
254
254
  def set_id
255
255
  namespace = self.class.sti_child? ? self.class.sti_base_absolute_namespace : self.class.absolute_namespace
256
- Modis.with_connection do |redis|
256
+ Modis.with_connection(modis_connection) do |redis|
257
257
  self.id = redis.incr("#{namespace}_id_seq")
258
258
  end
259
259
  end
@@ -8,7 +8,7 @@ module Modis
8
8
 
9
9
  module ClassMethods
10
10
  def transaction
11
- Modis.with_connection { |redis| redis.multi { |transaction| yield(transaction) } }
11
+ Modis.with_connection(modis_connection) { |redis| redis.multi { |transaction| yield(transaction) } }
12
12
  end
13
13
  end
14
14
  end
data/lib/modis/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Modis
4
- VERSION = '4.2.0'
4
+ VERSION = '4.3.0'
5
5
  end
data/lib/modis.rb CHANGED
@@ -7,6 +7,7 @@ require 'active_support/all'
7
7
  require 'msgpack'
8
8
 
9
9
  require 'modis/version'
10
+ require 'modis/deprecator'
10
11
  require 'modis/configuration'
11
12
  require 'modis/attribute'
12
13
  require 'modis/errors'
@@ -18,13 +19,20 @@ require 'modis/model'
18
19
 
19
20
  module Modis
20
21
  @mutex = Mutex.new
21
-
22
22
  class << self
23
- attr_writer :redis_options, :connection_pool_size, :connection_pool_timeout,
24
- :connection_pool
23
+ attr_writer :connection_pool_size, :connection_pool_timeout,
24
+ :connection_pools
25
25
 
26
26
  def redis_options
27
- @redis_options ||= {}
27
+ @redis_options ||= { default: {} }
28
+ end
29
+
30
+ def redis_options=(options)
31
+ if options.is_a?(Hash) && options.values.first.is_a?(Hash)
32
+ @redis_options = options.transform_values(&:dup)
33
+ else
34
+ @redis_options[:default] = options
35
+ end
28
36
  end
29
37
 
30
38
  def connection_pool_size
@@ -35,17 +43,25 @@ module Modis
35
43
  @connection_pool_timeout ||= 5
36
44
  end
37
45
 
38
- def connection_pool
39
- return @connection_pool if @connection_pool
46
+ def connection_pools
47
+ @connection_pools ||= {}
48
+ end
40
49
 
41
- @mutex.synchronize do
42
- options = { size: connection_pool_size, timeout: connection_pool_timeout }
43
- @connection_pool = ConnectionPool.new(options) { Redis.new(redis_options) }
50
+ def connection_pool(pool_name = :default)
51
+ connection_pools[pool_name] ||= begin
52
+ @mutex.synchronize do
53
+ ConnectionPool.new(
54
+ size: connection_pool_size,
55
+ timeout: connection_pool_timeout
56
+ ) do
57
+ Redis.new(redis_options[pool_name])
58
+ end
59
+ end
44
60
  end
45
61
  end
46
62
 
47
- def with_connection
48
- connection_pool.with { |connection| yield(connection) }
63
+ def with_connection(pool_name = :default)
64
+ connection_pool(pool_name).with { |connection| yield(connection) }
49
65
  end
50
66
  end
51
67
  end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MultiRedisSpec
4
+ class DefaultUserModel
5
+ include Modis::Model
6
+
7
+ attribute :name, :string
8
+ end
9
+
10
+ class CustomUserModel
11
+ include Modis::Model
12
+ self.modis_connection = :custom
13
+
14
+ attribute :name, :string
15
+ end
16
+ end
17
+
18
+ describe 'Multiple redis support' do
19
+ before do
20
+ Modis.redis_options = {
21
+ default: { url: 'redis://localhost:6379/0' },
22
+ custom: { url: 'redis://localhost:6379/1' }
23
+ }
24
+ end
25
+
26
+ it 'uses the default redis connection' do
27
+ expect(Modis).to receive(:with_connection).with(:default).at_least(3).times.and_call_original
28
+ user = MultiRedisSpec::DefaultUserModel.create!(name: 'Ian')
29
+
30
+ expect(Modis).to receive(:with_connection).with(:default).at_least(3).times.and_call_original
31
+ MultiRedisSpec::DefaultUserModel.find(user.id)
32
+ end
33
+
34
+ it 'uses the specified redis connection when set up' do
35
+ expect(Modis).to receive(:with_connection).with(:custom).at_least(3).times.and_call_original
36
+ user = MultiRedisSpec::CustomUserModel.create!(name: 'Tanya')
37
+
38
+ expect(Modis).to receive(:with_connection).with(:custom).at_least(3).times.and_call_original
39
+ MultiRedisSpec::CustomUserModel.find(user.id)
40
+ end
41
+ end
42
+
43
+ describe 'backwards compatibility' do
44
+ before do
45
+ Modis.redis_options = {
46
+ url: 'redis://localhost:6379/0'
47
+ }
48
+ end
49
+
50
+ it 'uses the default redis connection' do
51
+ expect(Modis).to receive(:with_connection).with(:default).at_least(3).times.and_call_original
52
+ MultiRedisSpec::DefaultUserModel.create!(name: 'Ian')
53
+ end
54
+ end
data/spec/spec_helper.rb CHANGED
@@ -20,9 +20,12 @@ end
20
20
 
21
21
  RSpec.configure do |config|
22
22
  config.after :each do
23
- Modis.with_connection do |connection|
24
- keys = connection.keys "#{Modis.config.namespace}:*"
25
- connection.del(*keys) unless keys.empty?
23
+ RSpec::Mocks.space.proxy_for(Modis).reset
24
+ Modis.connection_pools.each do |key, _|
25
+ Modis.with_connection(key) do |connection|
26
+ keys = connection.keys "#{Modis.config.namespace}:*"
27
+ connection.del(*keys) unless keys.empty?
28
+ end
26
29
  end
27
30
  end
28
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modis
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.0
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Leitch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-01 00:00:00.000000000 Z
11
+ date: 2024-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -236,6 +236,7 @@ files:
236
236
  - lib/modis.rb
237
237
  - lib/modis/attribute.rb
238
238
  - lib/modis/configuration.rb
239
+ - lib/modis/deprecator.rb
239
240
  - lib/modis/errors.rb
240
241
  - lib/modis/finder.rb
241
242
  - lib/modis/index.rb
@@ -249,6 +250,7 @@ files:
249
250
  - spec/errors_spec.rb
250
251
  - spec/finder_spec.rb
251
252
  - spec/index_spec.rb
253
+ - spec/multi_redis_spec.rb
252
254
  - spec/persistence_spec.rb
253
255
  - spec/spec_helper.rb
254
256
  - spec/support/simplecov_helper.rb
@@ -283,6 +285,7 @@ test_files:
283
285
  - spec/errors_spec.rb
284
286
  - spec/finder_spec.rb
285
287
  - spec/index_spec.rb
288
+ - spec/multi_redis_spec.rb
286
289
  - spec/persistence_spec.rb
287
290
  - spec/spec_helper.rb
288
291
  - spec/support/simplecov_helper.rb