ssdb-attr 0.1.6 → 0.2.3

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
- SHA1:
3
- metadata.gz: 4f3d5d24b28601b0a28fea83e2c94a42ceb84aa1
4
- data.tar.gz: 50c1fb44c11fec003ec6d0020b0f0d953c9fe417
2
+ SHA256:
3
+ metadata.gz: cf8bc6e43c0b5fb21814507308944afa6b29600cc14f2f4c88515d39ef6c4e50
4
+ data.tar.gz: 614888c10a52cb6803c56c92bfb699dd26451dc43c25a58683d89b0a4422e0ea
5
5
  SHA512:
6
- metadata.gz: 9bdf23f8f43482b91d1061010dd2f3e5e69bf57a5dc927c446f489c773716a12c17bfb1397a939b216d0eed7023b02a2f3f5b8beb699411765d705696552e4e3
7
- data.tar.gz: 29dbf077916d43e8b77bdde704c49834b36b47edd7a6d3e5f565671d7c7ac9e761ff632c18f402a84535653f8c90cdd0ad6857461f22018ed77549c73acf5937
6
+ metadata.gz: 6a3713508cf4b7b9361254caa29ebd77e1788a03da2aafefeca966d5837bf433b35bb3440f39db4bbed375eac16fb823c9cb37af40ee200b8b7af9bdd418a9e2
7
+ data.tar.gz: 7443900e14cf2515608c95bd34ea6b37550f4c16cb884b59524c6077e3a7e0cc6ea5e720c06743b2a30ebbc8d451e43b0fffca3037a07aad57732f86588c1ebb
@@ -1 +1 @@
1
- 2.3.4
1
+ 2.5.3
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "https://rubygems.org"
1
+ source "https://gems.ruby-china.com"
2
2
 
3
3
  gemspec
4
4
 
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ssdb-attr (0.1.6)
4
+ ssdb-attr (0.2.3)
5
5
  activerecord (>= 4.2.1, < 5.1)
6
6
  connection_pool (~> 2.2.0)
7
7
  redis (>= 3.2.1)
8
8
 
9
9
  GEM
10
- remote: https://rubygems.org/
10
+ remote: https://gems.ruby-china.com/
11
11
  specs:
12
12
  activemodel (5.0.5)
13
13
  activesupport (= 5.0.5)
@@ -25,7 +25,7 @@ GEM
25
25
  arel (7.1.4)
26
26
  coderay (1.1.1)
27
27
  concurrent-ruby (1.0.5)
28
- connection_pool (2.2.1)
28
+ connection_pool (2.2.2)
29
29
  diff-lcs (1.3)
30
30
  i18n (0.8.6)
31
31
  method_source (0.8.2)
@@ -35,7 +35,7 @@ GEM
35
35
  method_source (~> 0.8.1)
36
36
  slop (~> 3.4)
37
37
  rake (12.0.0)
38
- redis (3.2.2)
38
+ redis (4.1.0)
39
39
  rspec (3.6.0)
40
40
  rspec-core (~> 3.6.0)
41
41
  rspec-expectations (~> 3.6.0)
@@ -68,4 +68,4 @@ DEPENDENCIES
68
68
  ssdb-attr!
69
69
 
70
70
  BUNDLED WITH
71
- 1.15.3
71
+ 1.17.3
@@ -60,6 +60,15 @@ module SSDBAttr
60
60
 
61
61
  SSDBAttr.pools[pool_name.to_sym] = create_pool(conf)
62
62
  SSDBAttr.default_pool_name = pool_name if conf[:default]
63
+
64
+ slaves_conf = conf[:slaves]
65
+ if slaves_conf.present?
66
+ SSDBAttr.pools["#{pool_name}_slaves".to_sym] = []
67
+
68
+ slaves_conf.each do |slave|
69
+ SSDBAttr.pools["#{pool_name}_slaves".to_sym] << create_pool(slave.symbolize_keys)
70
+ end
71
+ end
63
72
  end
64
73
  end
65
74
 
@@ -71,6 +80,18 @@ module SSDBAttr
71
80
  SSDBAttr.pools[name.to_sym]
72
81
  end
73
82
 
83
+ def read_pool(name=nil)
84
+ name = name || SSDBAttr.default_pool_name
85
+
86
+ slaves = SSDBAttr.pools["#{name}_slaves".to_sym]
87
+ if !slaves.nil? && !slaves.empty?
88
+ slave_idx = rand(0..(slaves.size - 1))
89
+ return slaves[slave_idx]
90
+ end
91
+
92
+ SSDBAttr.pools[name.to_sym]
93
+ end
94
+
74
95
  def default_pool
75
96
  SSDBAttr.pools[SSDBAttr.default_pool_name]
76
97
  end
@@ -89,7 +110,11 @@ module SSDBAttr
89
110
  if !conn_options[:url].nil?
90
111
  Redis.new(:url => conn_options[:url])
91
112
  else
92
- Redis.new(:host => conn_options[:host], :port => conn_options[:port])
113
+ Redis.new(
114
+ :host => conn_options[:host],
115
+ :port => conn_options[:port],
116
+ :db => conn_options[:db] || 0
117
+ )
93
118
  end
94
119
  end
95
120
 
@@ -107,15 +132,21 @@ module SSDBAttr
107
132
  # @return [ActiveRecord::Relation|Array]
108
133
  #
109
134
  def load_attrs(objects, *fields)
135
+ return if objects.nil? || objects.empty?
136
+
110
137
  fields.map!(&:to_s)
111
138
 
112
139
  keys = objects.flat_map do |object|
140
+ next if object.nil?
113
141
  fields.map { |name| object.ssdb_attr_key(name) }
114
142
  end
115
- values = SSDBAttr.pool.with { |conn| conn.mget(keys) }
143
+
144
+ values = SSDBAttr.read_pool.with { |conn| conn.mget(keys) }
116
145
  key_values = keys.zip(values).to_h
117
146
 
118
147
  objects.each do |object|
148
+ next if object.nil?
149
+
119
150
  fields.each do |name|
120
151
  next unless object.class.ssdb_attr_names.include?(name.to_s)
121
152
 
@@ -1,3 +1,3 @@
1
1
  module SSDBAttr
2
- VERSION = "0.1.6"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -60,7 +60,12 @@ module SSDB
60
60
 
61
61
  define_method(name) do
62
62
  instance_variable_get("@#{name}") || begin
63
- val = ssdb_attr_pool.with { |conn| conn.get(ssdb_attr_key(name)) } || options[:default]
63
+ val = if ssdb_attr_id.nil?
64
+ options[:default]
65
+ else
66
+ ssdb_attr_read_pool.with { |conn| conn.get(ssdb_attr_key(name)) } || options[:default]
67
+ end
68
+
64
69
  instance_variable_set("@#{name}", typecaster(val, type))
65
70
  end
66
71
  end
@@ -75,15 +80,10 @@ module SSDB
75
80
  end
76
81
 
77
82
  define_method("#{name}_was") { attribute_was(name) }
78
-
79
83
  define_method("#{name}_change") { attribute_change(name) }
80
-
81
84
  define_method("#{name}_changed?") { attribute_changed?(name) }
82
-
83
85
  define_method("restore_#{name}!") { restore_attribute!(name) }
84
-
85
86
  define_method("#{name}_will_change!") { attribute_will_change!(name) }
86
-
87
87
  end
88
88
  end
89
89
 
@@ -108,7 +108,7 @@ module SSDB
108
108
  def load_ssdb_attrs(*fields)
109
109
  fields = (fields.map(&:to_s) & self.class.ssdb_attr_names)
110
110
 
111
- values = ssdb_attr_pool.with do |conn|
111
+ values = ssdb_attr_read_pool.with do |conn|
112
112
  conn.mget(fields.map { |name| ssdb_attr_key(name) })
113
113
  end
114
114
 
@@ -158,6 +158,22 @@ module SSDB
158
158
  SSDBAttr.pool(self.class.ssdb_attr_pool_name)
159
159
  end
160
160
 
161
+ #
162
+ # Return the ConnectionPool for reading purpose
163
+ #
164
+ #
165
+ # @return [ConnectionPool]
166
+ #
167
+ def ssdb_attr_read_pool
168
+ SSDBAttr.read_pool(self.class.ssdb_attr_pool_name)
169
+ end
170
+
171
+ #
172
+ # Return ssdb_attr_id for the current object
173
+ #
174
+ #
175
+ # @return [String]
176
+ #
161
177
  def ssdb_attr_id
162
178
  send(self.class.ssdb_attr_id_field || :id)
163
179
  end
@@ -200,7 +216,7 @@ module SSDB
200
216
  #
201
217
  def reload_ssdb_attrs
202
218
  keys = self.class.ssdb_attr_names.map { |name| ssdb_attr_key(name) }
203
- values = ssdb_attr_pool.with { |conn| conn.mget(keys) }
219
+ values = ssdb_attr_read_pool.with { |conn| conn.mget(keys) }
204
220
 
205
221
  self.class.ssdb_attr_names.each_with_index do |attr, index|
206
222
  instance_variable_set("@#{attr}", typecaster(values[index], self.class.ssdb_attr_definition[attr]))
@@ -64,6 +64,7 @@ class CustomIdField < ActiveRecord::Base
64
64
 
65
65
  ssdb_attr_id :uuid
66
66
  ssdb_attr :content, :string
67
+ ssdb_attr :title, :string, default: "foobar"
67
68
  end
68
69
 
69
70
  class CustomPoolName < ActiveRecord::Base
@@ -28,6 +28,36 @@ describe SSDBAttr do
28
28
  end
29
29
  end
30
30
 
31
+ describe "#read_pool" do
32
+ it "should fetch the reading slave pool if slave exists" do
33
+ options = [
34
+ {
35
+ :host => "localhost",
36
+ :port => "8888",
37
+ :name => "default",
38
+ :default => true,
39
+ :slaves => [
40
+ {
41
+ :host => "localhost",
42
+ :port => "6379",
43
+ :name => "slave0"
44
+ },
45
+ {
46
+ :host => "localhost",
47
+ :port => "6379",
48
+ :name => "slave1"
49
+ }
50
+ ]
51
+ }
52
+ ]
53
+
54
+ SSDBAttr.setup(options)
55
+ expect(SSDBAttr.pools[:default]).to be_a(ConnectionPool)
56
+ expect(SSDBAttr.pools[:default_slaves].size).to eq(2)
57
+ expect(SSDBAttr.pools[:default_slaves]).to include(SSDBAttr.read_pool)
58
+ end
59
+ end
60
+
31
61
  describe "#setup" do
32
62
  context "with only one pool" do
33
63
  it "should setup a ssdb connection pool with no name specified" do
@@ -129,6 +159,34 @@ describe SSDBAttr do
129
159
  end
130
160
 
131
161
  describe ".load_attrs" do
162
+ it "should not raise exception if objects is nil" do
163
+ expect {
164
+ SSDBAttr.load_attrs(nil, :name)
165
+ }.not_to raise_error
166
+ end
167
+
168
+ it "should not raise exception if objects contains nil" do
169
+ post1 = Post.create(:name => "lol", :version => 2)
170
+ post2 = Post.create(:name => "dota", :version => 3)
171
+
172
+ posts = Post.where(:id => [post1.id, nil, post2.id])
173
+
174
+ posts.each do |post|
175
+ expect(post.instance_variable_get(:@name)).to be(nil)
176
+ expect(post.instance_variable_get(:@version)).to be(nil)
177
+ end
178
+
179
+ expect {
180
+ SSDBAttr.load_attrs(posts, :name, :version)
181
+ }.not_to raise_error
182
+
183
+ expect(posts[0].instance_variable_get(:@name)).to eq("lol")
184
+ expect(posts[0].instance_variable_get(:@version)).to eq(2)
185
+
186
+ expect(posts[1].instance_variable_get(:@name)).to eq("dota")
187
+ expect(posts[1].instance_variable_get(:@version)).to eq(3)
188
+ end
189
+
132
190
  it "returns the values correctly" do
133
191
  post1 = Post.create(:name => "lol", :version => 2)
134
192
  post2 = Post.create(:name => "dota", :version => 3)
@@ -32,6 +32,11 @@ describe SSDB::Attr do
32
32
  end
33
33
 
34
34
  describe "#attribute=" do
35
+ it "should not request ssdb and return default directly if ssdb_attr_id is nil" do
36
+ post = Post.new
37
+ expect(post.default_title).to eq("Untitled")
38
+ end
39
+
35
40
  it "shouldn't change the attribute value in SSDB" do
36
41
  post.title = "foobar"
37
42
  expect(redis.get(post.send(:ssdb_attr_key, :title))).not_to eq("foobar")
@@ -188,6 +193,17 @@ describe SSDB::Attr do
188
193
  end
189
194
  end
190
195
 
196
+ context "ssdb_attr_id" do
197
+ context "id is nil" do
198
+ it "should return the default value" do
199
+ custom_field = CustomIdField.new
200
+ expect(custom_field).not_to receive(:ssdb_attr_read_pool)
201
+ expect(custom_field.uuid).to be_nil
202
+ expect(custom_field.title).to eq("foobar")
203
+ end
204
+ end
205
+ end
206
+
191
207
  context "CustomIdField" do
192
208
  let(:custom_id_field) { CustomIdField.create(:uuid => 123) }
193
209
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ssdb-attr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Larry Zhao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-30 00:00:00.000000000 Z
11
+ date: 2019-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  version: '0'
104
104
  requirements: []
105
105
  rubyforge_project:
106
- rubygems_version: 2.6.12
106
+ rubygems_version: 2.7.6
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: Provide simple helpers on persist values in redis for performance. Works