bigrails-redis 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0504974f840e1eed2db5ae02f3a08507cf6e220232d2011f3308ca99d856f7c2'
4
- data.tar.gz: 7e6f88e57302136b6f0b3c8c5ec5a4ddd23ecdbc07bdd2bc5b63c5d46ff1f082
3
+ metadata.gz: 8a2638608a5a80ec36cf93efccc3b761d9d1b976b0529dc407901f42c4269f7d
4
+ data.tar.gz: 677a8e2fbd028515253f3fac94a29b0cde604053cda39521fcc1efe6354e0020
5
5
  SHA512:
6
- metadata.gz: fe2c1cd64f7b06b90114a424978ebb1ee081be067c5916ade99e4b324e4e0128c2e5b3c47738614c0f67d2e5c125da518b67a418a36d7ed6af7e44f985c1737a
7
- data.tar.gz: 4393c6af1661d65fd7cf2f7d867aab8ae6f5e800d96d3e0f1db89d761af2f99d624fc932b3af4438576c3ebd639a8f9c57e9917ccfb9374b53026446f85585c4
6
+ metadata.gz: 41ea96f64d5f57dcb9f058420e007dc2e84a106ea9d7232402483c16105f3b42a6f96c33f4a2514c20cf961ee44a1b8bbad8790124c9c93ffa94a042e5d6137e
7
+ data.tar.gz: 561b0610a79041b670f4b11036ea0ee0c1fbad843580726f1e694f3c9695b5b93df6b02cf59cd222060cdf152dad7e0ff9300e8372ec568211e4fc829c99f2e7
data/README.md CHANGED
@@ -18,9 +18,15 @@ Create a redis configuration file:
18
18
 
19
19
  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.
20
20
 
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`.
22
22
 
23
23
  ```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
+
24
30
  # Simple hardcoded example.
25
31
  connection(:default) do
26
32
  {
@@ -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
@@ -5,24 +5,15 @@ module BigRails
5
5
  class Configuration
6
6
  attr_reader :redis_options
7
7
  attr_reader :pool_options
8
+ attr_reader :urls
8
9
 
9
10
  def initialize(redis_options)
10
11
  @redis_options = redis_options
12
+ @urls = Array(redis_options.delete(:url))
11
13
  @pool_options ||= {}.tap do |pool_options|
12
14
  pool_options[:size] = redis_options.delete(:pool_size) if redis_options[:pool_size]
13
15
  pool_options[:timeout] = redis_options.delete(:pool_timeout) if redis_options[:pool_timeout]
14
16
  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
26
17
  end
27
18
  end
28
19
  end
@@ -28,7 +28,7 @@ module BigRails
28
28
  raise ArgumentError, "connection named '#{name}' already registered"
29
29
  end
30
30
 
31
- @__configurations[name.to_s] = Configuration.new(yield)
31
+ @__configurations[name.to_s] = yield
32
32
  end
33
33
  end
34
34
  end
@@ -5,7 +5,6 @@ module BigRails
5
5
  class Registry
6
6
  class UnknownConnection < StandardError
7
7
  end
8
-
9
8
  class VerificationError < StandardError
10
9
  end
11
10
 
@@ -14,11 +13,7 @@ module BigRails
14
13
  def initialize
15
14
  @connections = {}
16
15
  @wrapped_connections = {}
17
-
18
- # Default redis builder.
19
- @builder = ->(config) {
20
- ActiveSupport::Cache::RedisCacheStore.build_redis(**config.redis_options)
21
- }
16
+ @builder = Builder
22
17
  end
23
18
 
24
19
  def for(name, wrapped: false)
@@ -71,12 +66,7 @@ module BigRails
71
66
 
72
67
  def build_connection(name)
73
68
  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)
80
70
  end
81
71
 
82
72
  def build_wrapped_connection(connection)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module BigRails
4
4
  module Redis
5
- VERSION = "0.5.0"
5
+ VERSION = "0.6.0"
6
6
  end
7
7
  end
@@ -8,8 +8,9 @@ module BigRails
8
8
  extend ActiveSupport::Autoload
9
9
 
10
10
  autoload :ApplicationExtension
11
- autoload :ConfigurationDsl
11
+ autoload :Builder
12
12
  autoload :Configuration
13
+ autoload :ConfigurationDsl
13
14
  autoload :Registry
14
15
  end
15
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bigrails-redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ngan Pham
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-22 00:00:00.000000000 Z
11
+ date: 2022-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4'
27
41
  description:
28
42
  email:
29
43
  - ngan@users.noreply.github.com
@@ -34,6 +48,7 @@ files:
34
48
  - README.md
35
49
  - lib/big_rails/redis.rb
36
50
  - lib/big_rails/redis/application_extension.rb
51
+ - lib/big_rails/redis/builder.rb
37
52
  - lib/big_rails/redis/configuration.rb
38
53
  - lib/big_rails/redis/configuration_dsl.rb
39
54
  - lib/big_rails/redis/railtie.rb