bigrails-redis 0.4.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4fad4c4ad81fa365e20f3d168ea4ddd8043b121d88e7d46ccc875e139e7049c
4
- data.tar.gz: ac86f0f454a81a3b040562c98dc74036c0b96f714b501dfd65a8df224870071f
3
+ metadata.gz: 7bcd1b5e5ed505a29938315709f56d36db7f5d5de17392657aa5c8371ab1d2c9
4
+ data.tar.gz: 9a51d0bc94aa4f0ead89243b9824afd94cb0e354edc8d43ca9833814944848fa
5
5
  SHA512:
6
- metadata.gz: d130159d4ae34ac9c432dd554a7222fe0f2eb118f71ae1c6bc427a29cab4a1e5ed5b7f83c0410fe99a8977e685105567fba5387b9f04a8ac45ee67f6228a75b5
7
- data.tar.gz: fe6b41f256a94205d9913a8faa00234b9ab105ce520875b3bccc6929274081788158da37a128efa0d91a38cba307c661a9e357d0fe7a8d596244db77c4d51799
6
+ metadata.gz: dd8ff8e9c4ee17ac8287f0d2da71be62ea36418ad6a4faa0841161402b7199ab870608d46df3d5a3e402343239a043fb211890136423ac800e5e68f9957c1730
7
+ data.tar.gz: ccdc2a87df4b34172c53b5038559c0a460b7bacba9bc2faf71ea69728522f1ef7dc5180de7c7e4ce63dcf23992a8746f0c46f39aa988bf1861aa4cfb17d70e2f
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,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "redis"
4
+ require "redis/distributed"
5
+
6
+ module BigRails
7
+ module Redis
8
+ class Builder
9
+ class << self
10
+ def call(options)
11
+ config = Configuration.new(options)
12
+
13
+ if config.pool_options.any?
14
+ ensure_connection_pool_added!
15
+
16
+ ::ConnectionPool.new(config.pool_options) { build(config) }
17
+ else
18
+ build(config)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def build(config)
25
+ if config.urls.size > 1
26
+ build_redis_distributed_client(urls: config.urls, **config.redis_options)
27
+ else
28
+ build_redis_client(url: config.urls.first, **config.redis_options)
29
+ end
30
+ end
31
+
32
+ def build_redis_distributed_client(urls:, **redis_options)
33
+ ::Redis::Distributed.new([], redis_options).tap do |dist|
34
+ urls.each { |u| dist.add_node(url: u) }
35
+ end
36
+ end
37
+
38
+ def build_redis_client(url:, **redis_options)
39
+ ::Redis.new(redis_options.merge(url: url))
40
+ end
41
+
42
+ def ensure_connection_pool_added!
43
+ require "connection_pool"
44
+ rescue LoadError
45
+ warn "You don't have connection_pool installed in your application. Please add it to your Gemfile and run bundle install"
46
+ raise
47
+ end
48
+ end
49
+ end
50
+ end
51
+ 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
@@ -14,11 +14,7 @@ module BigRails
14
14
  def initialize
15
15
  @connections = {}
16
16
  @wrapped_connections = {}
17
-
18
- # Default redis builder.
19
- @builder = ->(config) {
20
- ActiveSupport::Cache::RedisCacheStore.build_redis(**config.redis_options)
21
- }
17
+ @builder = Builder
22
18
  end
23
19
 
24
20
  def for(name, wrapped: false)
@@ -32,7 +28,7 @@ module BigRails
32
28
  end
33
29
 
34
30
  def config_for(name)
35
- configurations[validate_name(name)]
31
+ configurations[validate_name(name)].deep_dup
36
32
  end
37
33
 
38
34
  def each(&block)
@@ -42,9 +38,9 @@ module BigRails
42
38
  def disconnect
43
39
  each do |connection|
44
40
  if connection.is_a?(::ConnectionPool)
45
- connection.reload { |conn| conn.quit }
41
+ connection.reload { |conn| conn.close }
46
42
  else
47
- connection.quit
43
+ connection.close
48
44
  end
49
45
  end
50
46
  end
@@ -70,13 +66,7 @@ module BigRails
70
66
  private
71
67
 
72
68
  def build_connection(name)
73
- 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_for(name))
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.4.0"
5
+ VERSION = "0.7.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.4.0
4
+ version: 0.7.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-20 00:00:00.000000000 Z
11
+ date: 2022-03-24 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