-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for multiple redis connections (#49)
This PR adds support for more than one redis connection by providing multiple configurations to `redis_options` like this: ```ruby Modis.redis_options = { default: { url: 'redis://localhost:6379/0' }, custom: { url: 'redis://localhost:6379/1' } } ``` while still being backwards compatible to the old configuration style ```ruby Modis.redis_options = { url: 'redis://localhost:6379/0 } ``` You can then select which connection to use on a per model basis ```ruby class User include Modis::Model self.modis_connection = :custom attribute :name, :string end ``` If you have any ideas on how to improve the tests I am more than happy to change them. --------- Co-authored-by: Anton Rieder <aried3r@gmail.com> Co-authored-by: Ben Langfeld <ben@langfeld.me> Co-authored-by: Ben Langfeld <blangfeld@powerhrg.com>
- Loading branch information
1 parent
78b4d09
commit 7967970
Showing
8 changed files
with
94 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module MultiRedisSpec | ||
class DefaultUserModel | ||
include Modis::Model | ||
|
||
attribute :name, :string | ||
end | ||
|
||
class CustomUserModel | ||
include Modis::Model | ||
self.modis_connection = :custom | ||
|
||
attribute :name, :string | ||
end | ||
end | ||
|
||
describe 'Multiple redis support' do | ||
before do | ||
Modis.redis_options = { | ||
default: { url: 'redis://localhost:6379/0' }, | ||
custom: { url: 'redis://localhost:6379/1' } | ||
} | ||
end | ||
|
||
it 'uses the default redis connection' do | ||
expect(Modis).to receive(:with_connection).with(:default).at_least(3).times.and_call_original | ||
user = MultiRedisSpec::DefaultUserModel.create!(name: 'Ian') | ||
|
||
expect(Modis).to receive(:with_connection).with(:default).at_least(3).times.and_call_original | ||
MultiRedisSpec::DefaultUserModel.find(user.id) | ||
end | ||
|
||
it 'uses the specified redis connection when set up' do | ||
expect(Modis).to receive(:with_connection).with(:custom).at_least(3).times.and_call_original | ||
user = MultiRedisSpec::CustomUserModel.create!(name: 'Tanya') | ||
|
||
expect(Modis).to receive(:with_connection).with(:custom).at_least(3).times.and_call_original | ||
MultiRedisSpec::CustomUserModel.find(user.id) | ||
end | ||
end | ||
|
||
describe 'backwards compatibility' do | ||
before do | ||
Modis.redis_options = { | ||
url: 'redis://localhost:6379/0' | ||
} | ||
end | ||
|
||
it 'uses the default redis connection' do | ||
expect(Modis).to receive(:with_connection).with(:default).at_least(3).times.and_call_original | ||
MultiRedisSpec::DefaultUserModel.create!(name: 'Ian') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters