Skip to content

Use a default config if shared.yml isn't available #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ Additional configurations can be added under `config/redis/*.yml` and referenced

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.

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`.

### Redis support

Kredis works with Redis server 4.0+, with the [Redis Ruby](https://github.com/redis/redis-rb) client version 4.2+.
Expand Down
11 changes: 10 additions & 1 deletion lib/kredis/connections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
require "redis"

module Kredis::Connections
DEFAULT_REDIS_URL = "redis://127.0.0.1:6379/0"
DEFAULT_REDIS_TIMEOUT = 1

mattr_accessor :connections, default: Hash.new
mattr_accessor :configurator
mattr_accessor :connector, default: ->(config) { Redis.new(config) }

def configured_for(name)
connections[name] ||= Kredis.instrument :meta, message: "Connected to #{name}" do
connector.call configurator.config_for("redis/#{name}")
if configurator.root.join("config/redis/#{name}.yml").exist?
connector.call configurator.config_for("redis/#{name}")
elsif name == :shared
Redis.new url: ENV.fetch("REDIS_URL", DEFAULT_REDIS_URL), timeout: DEFAULT_REDIS_TIMEOUT
else
raise "No configuration found for #{name}"
end
end
end

Expand Down
29 changes: 29 additions & 0 deletions test/connections_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

require "test_helper"
require "yaml"

class ConnectionsTest < ActiveSupport::TestCase
setup { Kredis.connections = {} }
teardown { Kredis.namespace = nil }

test "clear all" do
Expand All @@ -26,4 +28,31 @@ class ConnectionsTest < ActiveSupport::TestCase
assert_nil integer.value
assert_equal "don't remove me", Kredis.configured_for(:shared).get("mykey")
end

test "config from file" do
fixture_config = YAML.load_file(Pathname.new(Dir.pwd).join("test/fixtures/config/redis/shared.yml"))["test"].symbolize_keys

Kredis.configurator.stub(:config_for, fixture_config) do
Kredis.configurator.stub(:root, Pathname.new(Dir.pwd).join("test/fixtures")) do
assert_match %r|redis://127.0.0.1:6379/4|, Kredis.redis.inspect
end
end
end

test "default config in env" do
ENV["REDIS_URL"] = "redis://127.0.0.1:6379/3"
assert_match %r|redis://127.0.0.1:6379/3|, Kredis.redis.inspect
ensure
ENV.delete("REDIS_URL")
end

test "default config without env" do
assert_match %r|redis://127.0.0.1:6379/0|, Kredis.redis.inspect
end

test "custom config is missing" do
assert_raises do
Kredis.configured_for(:missing).set "mykey", "won't get set"
end
end
end
3 changes: 3 additions & 0 deletions test/fixtures/config/redis/shared.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test:
url: redis://127.0.0.1:6379/4
timeout: 1
5 changes: 4 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

require "kredis"

Kredis.configurator = Class.new { def config_for(name) { db: "1" } end }.new
Kredis.configurator = Class.new do
def config_for(name) { db: "1" } end
def root() Pathname.new(".") end
end.new

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

Expand Down