Skip to content

Commit 8d3e2b4

Browse files
committed
Release 0.6.0
1 parent df834f1 commit 8d3e2b4

File tree

10 files changed

+75
-38
lines changed

10 files changed

+75
-38
lines changed

Gemfile.lock

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
PATH
22
remote: .
33
specs:
4-
bigrails-redis (0.5.0)
4+
bigrails-redis (0.6.0)
55
rails (>= 6)
6+
redis (>= 4)
67

78
GEM
89
remote: https://rubygems.org/

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ Create a redis configuration file:
1818

1919
The configuration file (`config/redis.rb`) is just a plain Ruby file that will be evaluated when a connection is requested. Use the `connection` DSL method to declare your connections. The method will yield a block and you're expected to return a configuration hash.
2020

21-
A configuration hash, by default, is passed to `ActiveSupport::Cache::RedisCacheStore.build_redis(...)`. This is a Rails supplied helper which allows for more options than demostrated above. You'll want to [check out its source](https://github.com/rails/rails/blob/main/activesupport/lib/active_support/cache/redis_cache_store.rb#L77-L100) to get a better idea of what it supports.
21+
The configuration hash is passed to the default `Builder`. You can customize the builder with your own object/proc that responds to `#call`.
2222

2323
```ruby
24+
# Change the default builder.
25+
Rails.application.redis.builder = ->(options) {
26+
# options is the hash returned from the connection block.
27+
Redis.new(...)
28+
}
29+
2430
# Simple hardcoded example.
2531
connection(:default) do
2632
{

bigrails-redis.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
2222
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
2323

2424
spec.add_dependency "rails", ">= 6"
25+
spec.add_dependency "redis", ">= 4"
2526
end

lib/big_rails/redis.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ module Redis
88
extend ActiveSupport::Autoload
99

1010
autoload :ApplicationExtension
11-
autoload :ConfigurationDsl
11+
autoload :Builder
1212
autoload :Configuration
13+
autoload :ConfigurationDsl
1314
autoload :Registry
1415
end
1516
end

lib/big_rails/redis/builder.rb

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require "redis"
4+
5+
module BigRails
6+
module Redis
7+
class Builder
8+
class << self
9+
def call(options)
10+
config = Configuration.new(options)
11+
12+
if config.pool_options.any?
13+
ensure_connection_pool_added!
14+
15+
::ConnectionPool.new(config.pool_options) { build(config) }
16+
else
17+
build(config)
18+
end
19+
end
20+
21+
private
22+
23+
def build(config)
24+
if config.urls.size > 1
25+
build_redis_distributed_client(urls: config.urls, **config.redis_options)
26+
else
27+
build_redis_client(url: config.urls.first, **config.redis_options)
28+
end
29+
end
30+
31+
def build_redis_distributed_client(urls:, **redis_options)
32+
::Redis::Distributed.new([], redis_options).tap do |dist|
33+
urls.each { |u| dist.add_node(url: u) }
34+
end
35+
end
36+
37+
def build_redis_client(url:, **redis_options)
38+
::Redis.new(redis_options.merge(url: url))
39+
end
40+
41+
def ensure_connection_pool_added!
42+
require "connection_pool"
43+
rescue LoadError => e
44+
warn "You don't have connection_pool installed in your application. Please add it to your Gemfile and run bundle install"
45+
raise
46+
end
47+
end
48+
end
49+
end
50+
end

lib/big_rails/redis/configuration.rb

+2-11
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,15 @@ module Redis
55
class Configuration
66
attr_reader :redis_options
77
attr_reader :pool_options
8+
attr_reader :urls
89

910
def initialize(redis_options)
1011
@redis_options = redis_options
12+
@urls = Array(redis_options.delete(:url))
1113
@pool_options ||= {}.tap do |pool_options|
1214
pool_options[:size] = redis_options.delete(:pool_size) if redis_options[:pool_size]
1315
pool_options[:timeout] = redis_options.delete(:pool_timeout) if redis_options[:pool_timeout]
1416
end
15-
16-
ensure_connection_pool_added! if pool_options.any?
17-
end
18-
19-
private
20-
21-
def ensure_connection_pool_added!
22-
require "connection_pool"
23-
rescue LoadError => e
24-
warn "You don't have connection_pool installed in your application. Please add it to your Gemfile and run bundle install"
25-
raise e
2617
end
2718
end
2819
end

lib/big_rails/redis/configuration_dsl.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def connection(name)
2828
raise ArgumentError, "connection named '#{name}' already registered"
2929
end
3030

31-
@__configurations[name.to_s] = Configuration.new(yield)
31+
@__configurations[name.to_s] = yield
3232
end
3333
end
3434
end

lib/big_rails/redis/registry.rb

+2-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module Redis
55
class Registry
66
class UnknownConnection < StandardError
77
end
8-
98
class VerificationError < StandardError
109
end
1110

@@ -14,11 +13,7 @@ class VerificationError < StandardError
1413
def initialize
1514
@connections = {}
1615
@wrapped_connections = {}
17-
18-
# Default redis builder.
19-
@builder = ->(config) {
20-
ActiveSupport::Cache::RedisCacheStore.build_redis(**config.redis_options)
21-
}
16+
@builder = Builder
2217
end
2318

2419
def for(name, wrapped: false)
@@ -71,12 +66,7 @@ def verify!(*names)
7166

7267
def build_connection(name)
7368
config = configurations.fetch(name)
74-
75-
if config.pool_options.any?
76-
::ConnectionPool.new(config.pool_options) { builder.call(config) }
77-
else
78-
builder.call(config)
79-
end
69+
builder.call(config)
8070
end
8171

8272
def build_wrapped_connection(connection)

lib/big_rails/redis/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module BigRails
44
module Redis
5-
VERSION = "0.5.0"
5+
VERSION = "0.6.0"
66
end
77
end

spec/big_rails/redis/registry_spec.rb

+7-10
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,24 @@
4343

4444
describe "#config_for" do
4545
it "returns config for specific connection" do
46-
expect(instance.config_for("default").redis_options).to eq(
46+
expect(instance.config_for("default")).to eq(
4747
url: "redis://localhost"
4848
)
4949

50-
expect(instance.config_for("pooled").redis_options).to eq(
51-
url: "redis://localhost/2"
52-
)
53-
54-
expect(instance.config_for("pooled").pool_options).to eq(
55-
timeout: 5,
56-
size: 5
50+
expect(instance.config_for("pooled")).to eq(
51+
url: "redis://localhost/2",
52+
pool_timeout: 5,
53+
pool_size: 5
5754
)
5855
end
5956
end
6057

6158
describe "#each" do
6259
it "iterates through all connections" do
6360
instance.each do |conn|
64-
if conn.is_a? ::Redis
61+
if conn.is_a?(::Redis)
6562
expect(conn).to eq(instance.for("default"))
66-
elsif conn.is_a? ::ConnectionPool
63+
elsif conn.is_a?(::ConnectionPool)
6764
expect(conn).to eq(instance.for("pooled"))
6865
end
6966
end

0 commit comments

Comments
 (0)