Skip to content

Commit d048de3

Browse files
Add option to configure how the redis client is created (#46)
1 parent 73b99e1 commit d048de3

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,16 @@ Kredis::Connections.connections[:shared] = Redis.new(
243243

244244
The above code could be added to either `config/environments/production.rb` or an initializer. Please ensure that your client private key, if used, is stored your credentials file or another secure location.
245245

246+
### Configure how the redis client is created
247+
248+
You can configure how the redis client is created by setting `config.connector` in your `application.rb`:
249+
250+
```ruby
251+
config.kredis.connector = ->(config) { SomeRedisProxy.new(config) }
252+
```
253+
254+
By default Kredis will use `Redis.new(config)`.
255+
246256
## License
247257

248258
Kredis is released under the [MIT License](https://opensource.org/licenses/MIT).

lib/kredis/connections.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
module Kredis::Connections
44
mattr_accessor :connections, default: Hash.new
55
mattr_accessor :configurator
6+
mattr_accessor :connector, default: ->(config) { Redis.new(config) }
67

78
def configured_for(name)
89
connections[name] ||= begin
910
Kredis.instrument :meta, message: "Connected to #{name}" do
10-
Redis.new configurator.config_for("redis/#{name}")
11+
connector.call configurator.config_for("redis/#{name}")
1112
end
1213
end
1314
end

lib/kredis/railtie.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class Kredis::Railtie < ::Rails::Railtie
1212
Kredis::LogSubscriber.logger = config.kredis.logger || Rails.logger
1313
end
1414

15+
initializer "kredis.configuration" do
16+
Kredis::Connections.connector = config.kredis.connector || ->(config) { Redis.new(config) }
17+
end
18+
1519
initializer "kredis.configurator" do
1620
Kredis.configurator = Rails.application
1721
end

0 commit comments

Comments
 (0)