bigrails-redis 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -1
- data/lib/big_rails/redis/builder.rb +50 -0
- data/lib/big_rails/redis/configuration.rb +2 -11
- data/lib/big_rails/redis/configuration_dsl.rb +1 -1
- data/lib/big_rails/redis/registry.rb +2 -12
- data/lib/big_rails/redis/version.rb +1 -1
- data/lib/big_rails/redis.rb +2 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a2638608a5a80ec36cf93efccc3b761d9d1b976b0529dc407901f42c4269f7d
|
4
|
+
data.tar.gz: 677a8e2fbd028515253f3fac94a29b0cde604053cda39521fcc1efe6354e0020
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
@@ -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)
|
data/lib/big_rails/redis.rb
CHANGED
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
|
+
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-
|
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
|