Skip to content

Commit 2c7f41f

Browse files
authored
Use a default config if shared.yml isn't available (#135)
* Use a default config if shared.yml isn't available * Update README.md * Not used * Only use a default for the shared config
1 parent 2ccc5c6 commit 2c7f41f

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ Additional configurations can be added under `config/redis/*.yml` and referenced
245245

246246
Kredis passes the configuration to `Redis.new` to establish the connection. See the [Redis documentation](https://github.com/redis/redis-rb) for other configuration options.
247247

248+
If you don't have `config/redis/shared.yml` (or use another named configuration), Kredis will default to look in env for `REDIS_URL`, then fallback to a default URL of `redis://127.0.0.1:6379/0`.
249+
248250
### Redis support
249251

250252
Kredis works with Redis server 4.0+, with the [Redis Ruby](https://github.com/redis/redis-rb) client version 4.2+.

lib/kredis/connections.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33
require "redis"
44

55
module Kredis::Connections
6+
DEFAULT_REDIS_URL = "redis://127.0.0.1:6379/0"
7+
DEFAULT_REDIS_TIMEOUT = 1
8+
69
mattr_accessor :connections, default: Hash.new
710
mattr_accessor :configurator
811
mattr_accessor :connector, default: ->(config) { Redis.new(config) }
912

1013
def configured_for(name)
1114
connections[name] ||= Kredis.instrument :meta, message: "Connected to #{name}" do
12-
connector.call configurator.config_for("redis/#{name}")
15+
if configurator.root.join("config/redis/#{name}.yml").exist?
16+
connector.call configurator.config_for("redis/#{name}")
17+
elsif name == :shared
18+
Redis.new url: ENV.fetch("REDIS_URL", DEFAULT_REDIS_URL), timeout: DEFAULT_REDIS_TIMEOUT
19+
else
20+
raise "No configuration found for #{name}"
21+
end
1322
end
1423
end
1524

test/connections_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# frozen_string_literal: true
22

33
require "test_helper"
4+
require "yaml"
45

56
class ConnectionsTest < ActiveSupport::TestCase
7+
setup { Kredis.connections = {} }
68
teardown { Kredis.namespace = nil }
79

810
test "clear all" do
@@ -26,4 +28,31 @@ class ConnectionsTest < ActiveSupport::TestCase
2628
assert_nil integer.value
2729
assert_equal "don't remove me", Kredis.configured_for(:shared).get("mykey")
2830
end
31+
32+
test "config from file" do
33+
fixture_config = YAML.load_file(Pathname.new(Dir.pwd).join("test/fixtures/config/redis/shared.yml"))["test"].symbolize_keys
34+
35+
Kredis.configurator.stub(:config_for, fixture_config) do
36+
Kredis.configurator.stub(:root, Pathname.new(Dir.pwd).join("test/fixtures")) do
37+
assert_match %r|redis://127.0.0.1:6379/4|, Kredis.redis.inspect
38+
end
39+
end
40+
end
41+
42+
test "default config in env" do
43+
ENV["REDIS_URL"] = "redis://127.0.0.1:6379/3"
44+
assert_match %r|redis://127.0.0.1:6379/3|, Kredis.redis.inspect
45+
ensure
46+
ENV.delete("REDIS_URL")
47+
end
48+
49+
test "default config without env" do
50+
assert_match %r|redis://127.0.0.1:6379/0|, Kredis.redis.inspect
51+
end
52+
53+
test "custom config is missing" do
54+
assert_raises do
55+
Kredis.configured_for(:missing).set "mykey", "won't get set"
56+
end
57+
end
2958
end

test/fixtures/config/redis/shared.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test:
2+
url: redis://127.0.0.1:6379/4
3+
timeout: 1

test/test_helper.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
require "kredis"
1111

12-
Kredis.configurator = Class.new { def config_for(name) { db: "1" } end }.new
12+
Kredis.configurator = Class.new do
13+
def config_for(name) { db: "1" } end
14+
def root() Pathname.new(".") end
15+
end.new
1316

1417
ActiveSupport::LogSubscriber.logger = ActiveSupport::Logger.new(STDOUT) if ENV["VERBOSE"]
1518

0 commit comments

Comments
 (0)