second_level_cache 2.3.3 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +29 -22
- data/Gemfile +4 -2
- data/README.md +7 -1
- data/Rakefile +6 -4
- data/lib/second_level_cache.rb +7 -5
- data/lib/second_level_cache/active_record.rb +15 -10
- data/lib/second_level_cache/active_record/base.rb +2 -0
- data/lib/second_level_cache/active_record/belongs_to_association.rb +2 -0
- data/lib/second_level_cache/active_record/core.rb +2 -0
- data/lib/second_level_cache/active_record/fetch_by_uniq_key.rb +5 -3
- data/lib/second_level_cache/active_record/finder_methods.rb +3 -1
- data/lib/second_level_cache/active_record/has_one_association.rb +2 -0
- data/lib/second_level_cache/active_record/persistence.rb +2 -0
- data/lib/second_level_cache/active_record/preloader.rb +7 -4
- data/lib/second_level_cache/config.rb +3 -1
- data/lib/second_level_cache/mixin.rb +3 -1
- data/lib/second_level_cache/record_marshal.rb +2 -0
- data/lib/second_level_cache/record_relation.rb +2 -0
- data/lib/second_level_cache/version.rb +1 -1
- data/second_level_cache.gemspec +4 -4
- data/test/active_record_test_case_helper.rb +7 -5
- data/test/base_test.rb +6 -4
- data/test/belongs_to_association_test.rb +4 -2
- data/test/enum_attr_test.rb +6 -4
- data/test/fetch_by_uniq_key_test.rb +15 -13
- data/test/finder_methods_test.rb +6 -4
- data/test/has_one_association_test.rb +10 -8
- data/test/model/account.rb +2 -0
- data/test/model/animal.rb +2 -0
- data/test/model/book.rb +2 -0
- data/test/model/image.rb +2 -0
- data/test/model/order.rb +2 -0
- data/test/model/order_item.rb +2 -0
- data/test/model/post.rb +2 -0
- data/test/model/topic.rb +2 -0
- data/test/model/user.rb +6 -4
- data/test/persistence_test.rb +12 -10
- data/test/polymorphic_association_test.rb +4 -2
- data/test/preloader_non_integer_test.rb +17 -14
- data/test/preloader_test.rb +13 -10
- data/test/record_marshal_test.rb +17 -15
- data/test/require_test.rb +7 -5
- data/test/second_level_cache_test.rb +5 -3
- data/test/single_table_inheritance_test.rb +3 -1
- data/test/test_helper.rb +19 -17
- metadata +19 -19
data/test/base_test.rb
CHANGED
@@ -1,17 +1,19 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
2
4
|
|
3
5
|
class BaseTest < ActiveSupport::TestCase
|
4
6
|
def setup
|
5
|
-
@user = User.create name:
|
7
|
+
@user = User.create name: "csdn", email: "test@csdn.com"
|
6
8
|
end
|
7
9
|
|
8
10
|
def test_should_update_cache_when_update_attributes
|
9
|
-
@user.update_attributes name:
|
11
|
+
@user.update_attributes name: "change"
|
10
12
|
assert_equal @user.name, User.read_second_level_cache(@user.id).name
|
11
13
|
end
|
12
14
|
|
13
15
|
def test_should_update_cache_when_update_attribute
|
14
|
-
@user.update_attribute :name,
|
16
|
+
@user.update_attribute :name, "change"
|
15
17
|
assert_equal @user.name, User.read_second_level_cache(@user.id).name
|
16
18
|
end
|
17
19
|
|
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
2
4
|
|
3
5
|
class BelongsToAssociationTest < ActiveSupport::TestCase
|
4
6
|
def setup
|
5
|
-
@user = User.create name:
|
7
|
+
@user = User.create name: "csdn", email: "test@csdn.com"
|
6
8
|
end
|
7
9
|
|
8
10
|
def test_should_get_cache_when_use_belongs_to_association
|
data/test/enum_attr_test.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
2
4
|
|
3
5
|
class EnumAttrTest < ActiveSupport::TestCase
|
4
6
|
def setup
|
5
|
-
@user = User.create name:
|
7
|
+
@user = User.create name: "csdn", email: "test@csdn.com"
|
6
8
|
end
|
7
9
|
|
8
10
|
def test_enum_attr
|
9
11
|
@user.archived!
|
10
|
-
assert_equal
|
11
|
-
assert_equal
|
12
|
+
assert_equal "archived", @user.status
|
13
|
+
assert_equal "archived", User.find(@user.id).status
|
12
14
|
end
|
13
15
|
end
|
@@ -1,42 +1,44 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
2
4
|
|
3
5
|
class FetchByUinqKeyTest < ActiveSupport::TestCase
|
4
6
|
def setup
|
5
|
-
@user = User.create name:
|
6
|
-
@post = Post.create slug:
|
7
|
+
@user = User.create name: "hooopo", email: "hoooopo@gmail.com"
|
8
|
+
@post = Post.create slug: "foobar", topic_id: 2
|
7
9
|
end
|
8
10
|
|
9
11
|
def test_cache_uniq_key
|
10
|
-
assert_equal User.send(:cache_uniq_key, name:
|
11
|
-
assert_equal User.send(:cache_uniq_key, foo: 1, bar: 2),
|
12
|
-
assert_equal User.send(:cache_uniq_key, foo: 1, bar: nil),
|
13
|
-
long_val =
|
12
|
+
assert_equal User.send(:cache_uniq_key, name: "hooopo"), "uniq_key_User_name_hooopo"
|
13
|
+
assert_equal User.send(:cache_uniq_key, foo: 1, bar: 2), "uniq_key_User_foo_1,bar_2"
|
14
|
+
assert_equal User.send(:cache_uniq_key, foo: 1, bar: nil), "uniq_key_User_foo_1,bar_"
|
15
|
+
long_val = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
14
16
|
assert_equal User.send(:cache_uniq_key, foo: 1, bar: long_val), "uniq_key_User_foo_1,bar_#{Digest::MD5.hexdigest(long_val)}"
|
15
17
|
end
|
16
18
|
|
17
19
|
def test_should_query_from_db_using_primary_key
|
18
|
-
Post.fetch_by_uniq_keys(topic_id: 2, slug:
|
20
|
+
Post.fetch_by_uniq_keys(topic_id: 2, slug: "foobar")
|
19
21
|
@post.expire_second_level_cache
|
20
22
|
assert_sql(/SELECT\s+"posts".* FROM "posts"\s+WHERE "posts"."id" = \? LIMIT ?/) do
|
21
|
-
Post.fetch_by_uniq_keys(topic_id: 2, slug:
|
23
|
+
Post.fetch_by_uniq_keys(topic_id: 2, slug: "foobar")
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
25
27
|
def test_should_not_hit_db_using_fetch_by_uniq_key_twice
|
26
|
-
post = Post.fetch_by_uniq_keys(topic_id: 2, slug:
|
28
|
+
post = Post.fetch_by_uniq_keys(topic_id: 2, slug: "foobar")
|
27
29
|
assert_equal post, @post
|
28
30
|
assert_no_queries do
|
29
|
-
Post.fetch_by_uniq_keys(topic_id: 2, slug:
|
31
|
+
Post.fetch_by_uniq_keys(topic_id: 2, slug: "foobar")
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
35
|
def test_should_fail_when_fetch_by_uniq_key_with_bang_method
|
34
36
|
assert_raises(ActiveRecord::RecordNotFound) do
|
35
|
-
Post.fetch_by_uniq_keys!(topic_id: 2, slug:
|
37
|
+
Post.fetch_by_uniq_keys!(topic_id: 2, slug: "foobar1")
|
36
38
|
end
|
37
39
|
|
38
40
|
assert_raises(ActiveRecord::RecordNotFound) do
|
39
|
-
User.fetch_by_uniq_key!(
|
41
|
+
User.fetch_by_uniq_key!("xxxxx", :name)
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
data/test/finder_methods_test.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
2
4
|
|
3
5
|
class FinderMethodsTest < ActiveSupport::TestCase
|
4
6
|
def setup
|
5
|
-
@user = User.create name:
|
7
|
+
@user = User.create name: "csdn", email: "test@csdn.com"
|
6
8
|
end
|
7
9
|
|
8
10
|
def test_should_find_without_cache
|
@@ -34,14 +36,14 @@ class FinderMethodsTest < ActiveSupport::TestCase
|
|
34
36
|
|
35
37
|
def test_should_not_find_from_cache_when_select_speical_columns
|
36
38
|
@user.write_second_level_cache
|
37
|
-
only_id_user = User.select(
|
39
|
+
only_id_user = User.select("id").find(@user.id)
|
38
40
|
assert_raises(ActiveModel::MissingAttributeError) do
|
39
41
|
only_id_user.name
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
43
45
|
def test_without_second_level_cache
|
44
|
-
@user.name =
|
46
|
+
@user.name = "NewName"
|
45
47
|
@user.write_second_level_cache
|
46
48
|
User.without_second_level_cache do
|
47
49
|
@from_db = User.find(@user.id)
|
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
2
4
|
|
3
5
|
class HasOneAssociationTest < ActiveSupport::TestCase
|
4
6
|
def setup
|
5
|
-
@user = User.create name:
|
7
|
+
@user = User.create name: "hooopo", email: "hoooopo@gmail.com"
|
6
8
|
@account = @user.create_account
|
7
9
|
end
|
8
10
|
|
@@ -14,7 +16,7 @@ class HasOneAssociationTest < ActiveSupport::TestCase
|
|
14
16
|
end
|
15
17
|
|
16
18
|
def test_should_fetch_has_one_through
|
17
|
-
user = User.create name:
|
19
|
+
user = User.create name: "hooopo", email: "hoooopo@gmail.com", forked_from_user: @user
|
18
20
|
clean_user = user.reload
|
19
21
|
assert_equal User, clean_user.forked_from_user.class
|
20
22
|
assert_equal @user.id, user.forked_from_user.id
|
@@ -25,13 +27,13 @@ class HasOneAssociationTest < ActiveSupport::TestCase
|
|
25
27
|
end
|
26
28
|
|
27
29
|
def test_has_one_with_conditions
|
28
|
-
user = User.create name:
|
29
|
-
Namespace.create(user_id: user.id, name:
|
30
|
-
user.create_namespace(name:
|
31
|
-
Namespace.create(user_id: user.id, name:
|
30
|
+
user = User.create name: "hooopo", email: "hoooopo@gmail.com"
|
31
|
+
Namespace.create(user_id: user.id, name: "ruby-china", kind: "group")
|
32
|
+
user.create_namespace(name: "hooopo")
|
33
|
+
Namespace.create(user_id: user.id, name: "rails", kind: "group")
|
32
34
|
assert_not_equal user.namespace, nil
|
33
35
|
clear_user = User.find(user.id)
|
34
|
-
assert_equal clear_user.namespace.name,
|
36
|
+
assert_equal clear_user.namespace.name, "hooopo"
|
35
37
|
end
|
36
38
|
|
37
39
|
def test_assign_relation
|
data/test/model/account.rb
CHANGED
data/test/model/animal.rb
CHANGED
data/test/model/book.rb
CHANGED
data/test/model/image.rb
CHANGED
data/test/model/order.rb
CHANGED
data/test/model/order_item.rb
CHANGED
data/test/model/post.rb
CHANGED
data/test/model/topic.rb
CHANGED
data/test/model/user.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
ActiveRecord::Base.connection.create_table(:users, force: true) do |t|
|
2
4
|
t.text :options
|
3
5
|
t.text :json_options
|
@@ -28,11 +30,11 @@ class User < ActiveRecord::Base
|
|
28
30
|
second_level_cache(version: CACHE_VERSION, expires_in: 3.days)
|
29
31
|
|
30
32
|
serialize :options, Array
|
31
|
-
serialize :json_options, JSON if ::ActiveRecord::VERSION::STRING >=
|
33
|
+
serialize :json_options, JSON if ::ActiveRecord::VERSION::STRING >= "4.1.0"
|
32
34
|
store :extras, accessors: %i[tagline gender]
|
33
35
|
|
34
36
|
has_one :account
|
35
|
-
has_one :forked_user_link, foreign_key:
|
37
|
+
has_one :forked_user_link, foreign_key: "forked_to_user_id"
|
36
38
|
has_one :forked_from_user, through: :forked_user_link
|
37
39
|
has_many :namespaces
|
38
40
|
has_one :namespace, -> { where(kind: nil) }
|
@@ -49,6 +51,6 @@ class Namespace < ActiveRecord::Base
|
|
49
51
|
end
|
50
52
|
|
51
53
|
class ForkedUserLink < ActiveRecord::Base
|
52
|
-
belongs_to :forked_from_user, class_name:
|
53
|
-
belongs_to :forked_to_user, class_name:
|
54
|
+
belongs_to :forked_from_user, class_name: "User"
|
55
|
+
belongs_to :forked_to_user, class_name: "User"
|
54
56
|
end
|
data/test/persistence_test.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
2
4
|
|
3
5
|
class PersistenceTest < ActiveSupport::TestCase
|
4
6
|
def setup
|
5
|
-
@user = User.create name:
|
6
|
-
@topic = Topic.create title:
|
7
|
+
@user = User.create name: "csdn", email: "test@csdn.com"
|
8
|
+
@topic = Topic.create title: "csdn"
|
7
9
|
end
|
8
10
|
|
9
11
|
def test_should_reload_object
|
10
|
-
User.where(id: @user.id).update_all(email:
|
11
|
-
assert_equal
|
12
|
+
User.where(id: @user.id).update_all(email: "different@csdn.com")
|
13
|
+
assert_equal "different@csdn.com", @user.reload.email
|
12
14
|
end
|
13
15
|
|
14
16
|
def test_should_reload_object_associations
|
@@ -26,31 +28,31 @@ class PersistenceTest < ActiveSupport::TestCase
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def test_should_update_cache_after_update_column
|
29
|
-
@user.update_column :name,
|
31
|
+
@user.update_column :name, "new_name"
|
30
32
|
new_user = User.find @user.id
|
31
33
|
assert_equal new_user, @user
|
32
34
|
end
|
33
35
|
|
34
36
|
def test_should_update_cache_after_update_columns
|
35
|
-
@user.update_columns name:
|
37
|
+
@user.update_columns name: "new_name1"
|
36
38
|
new_user = User.find @user.id
|
37
39
|
assert_equal new_user, @user
|
38
40
|
end
|
39
41
|
|
40
42
|
def test_should_update_cache_after_update_attribute
|
41
|
-
@user.update_attribute :name,
|
43
|
+
@user.update_attribute :name, "new_name"
|
42
44
|
new_user = User.find @user.id
|
43
45
|
assert_equal new_user, @user
|
44
46
|
end
|
45
47
|
|
46
48
|
def test_should_update_cache_after_update
|
47
|
-
@user.update name:
|
49
|
+
@user.update name: "new_name"
|
48
50
|
new_user = User.find @user.id
|
49
51
|
assert_equal new_user, @user
|
50
52
|
end
|
51
53
|
|
52
54
|
def test_should_update_cache_after_update!
|
53
|
-
@user.update! name:
|
55
|
+
@user.update! name: "new_name"
|
54
56
|
new_user = User.find @user.id
|
55
57
|
assert_equal new_user, @user
|
56
58
|
end
|
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
2
4
|
|
3
5
|
class PolymorphicAssociationTest < ActiveSupport::TestCase
|
4
6
|
def setup
|
5
|
-
@user = User.create name:
|
7
|
+
@user = User.create name: "csdn", email: "test@csdn.com"
|
6
8
|
end
|
7
9
|
|
8
10
|
def test_should_get_cache_when_use_polymorphic_association
|
@@ -1,26 +1,28 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
2
4
|
|
3
5
|
class PreloaderNonIntegerTest < ActiveSupport::TestCase
|
4
6
|
def test_belongs_to_preload_caches_includes_uuid
|
5
7
|
orders = [
|
6
|
-
Order.create(id:
|
7
|
-
Order.create(id:
|
8
|
-
Order.create(id:
|
8
|
+
Order.create(id: "15944214-e4df-4e46-8d56-1f5864a0b90c", title: "title1", body: "body1"),
|
9
|
+
Order.create(id: "25944214-e4df-4e46-8d56-1f5864a0b90c", title: "title2", body: "body2"),
|
10
|
+
Order.create(id: "35944214-e4df-4e46-8d56-1f5864a0b90c", title: "title3", body: "body3")
|
9
11
|
]
|
10
12
|
orders.each_with_index { |order, i| order.order_items.create(body: "order_item#{order.id}", id: "1#{i}944214-e4df-4e46-8d56-1f5864a0b90c") }
|
11
13
|
|
12
14
|
results = nil
|
13
15
|
assert_queries(1) do
|
14
|
-
results = OrderItem.includes(:order).order(
|
16
|
+
results = OrderItem.includes(:order).order("id ASC").to_a
|
15
17
|
end
|
16
18
|
assert_equal orders, results.map(&:order)
|
17
19
|
end
|
18
20
|
|
19
21
|
def test_belongs_to_when_read_multi_missed_from_cache_ar_will_fetch_missed_records_from_db_uuid
|
20
22
|
orders = [
|
21
|
-
Order.create(id:
|
22
|
-
Order.create(id:
|
23
|
-
Order.create(id:
|
23
|
+
Order.create(id: "15944214-e4df-4e46-8d56-1f5864a0b90c", title: "title1", body: "body1"),
|
24
|
+
Order.create(id: "25944214-e4df-4e46-8d56-1f5864a0b90c", title: "title2", body: "body2"),
|
25
|
+
Order.create(id: "35944214-e4df-4e46-8d56-1f5864a0b90c", title: "title3", body: "body3")
|
24
26
|
]
|
25
27
|
orders.each_with_index { |order, i| order.order_items.create(body: "order_item#{order.id}", id: "1#{i}944214-e4df-4e46-8d56-1f5864a0b90c") }
|
26
28
|
expired_order = orders.first
|
@@ -28,8 +30,9 @@ class PreloaderNonIntegerTest < ActiveSupport::TestCase
|
|
28
30
|
|
29
31
|
results = nil
|
30
32
|
assert_queries(2) do
|
31
|
-
assert_sql(/WHERE\s\"orders\"\.\"id\"
|
32
|
-
results = OrderItem.includes(:order).order(
|
33
|
+
assert_sql(/WHERE\s\"orders\"\.\"id\" = ?/m) do
|
34
|
+
results = OrderItem.includes(:order).order("id ASC").to_a
|
35
|
+
assert_equal expired_order, results.first.order
|
33
36
|
end
|
34
37
|
end
|
35
38
|
|
@@ -37,11 +40,11 @@ class PreloaderNonIntegerTest < ActiveSupport::TestCase
|
|
37
40
|
end
|
38
41
|
|
39
42
|
def test_has_many_preloader_returns_correct_results
|
40
|
-
order = Order.create(id:
|
41
|
-
OrderItem.create(id:
|
42
|
-
order_item = order.order_items.create(id:
|
43
|
+
order = Order.create(id: "15944214-e4df-4e46-8d56-1f5864a0b90c")
|
44
|
+
OrderItem.create(id: "11944214-e4df-4e46-8d56-1f5864a0b90c")
|
45
|
+
order_item = order.order_items.create(id: "12944214-e4df-4e46-8d56-1f5864a0b90c")
|
43
46
|
|
44
|
-
assert_equal [order_item], Order.includes(:order_items).find(
|
47
|
+
assert_equal [order_item], Order.includes(:order_items).find("15944214-e4df-4e46-8d56-1f5864a0b90c").order_items
|
45
48
|
end
|
46
49
|
|
47
50
|
def test_has_one_preloader_returns_correct_results
|
data/test/preloader_test.rb
CHANGED
@@ -1,26 +1,28 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
2
4
|
|
3
5
|
class PreloaderTest < ActiveSupport::TestCase
|
4
6
|
def test_belongs_to_preload_caches_includes
|
5
7
|
topics = [
|
6
|
-
Topic.create(title:
|
7
|
-
Topic.create(title:
|
8
|
-
Topic.create(title:
|
8
|
+
Topic.create(title: "title1", body: "body1"),
|
9
|
+
Topic.create(title: "title2", body: "body2"),
|
10
|
+
Topic.create(title: "title3", body: "body3")
|
9
11
|
]
|
10
12
|
topics.each { |topic| topic.posts.create(body: "post#{topic.id}") }
|
11
13
|
|
12
14
|
results = nil
|
13
15
|
assert_queries(1) do
|
14
|
-
results = Post.includes(:topic).order(
|
16
|
+
results = Post.includes(:topic).order("id ASC").to_a
|
15
17
|
end
|
16
18
|
assert_equal topics, results.map(&:topic)
|
17
19
|
end
|
18
20
|
|
19
21
|
def test_belongs_to_when_read_multi_missed_from_cache_ar_will_fetch_missed_records_from_db
|
20
22
|
topics = [
|
21
|
-
Topic.create(title:
|
22
|
-
Topic.create(title:
|
23
|
-
Topic.create(title:
|
23
|
+
Topic.create(title: "title1", body: "body1"),
|
24
|
+
Topic.create(title: "title2", body: "body2"),
|
25
|
+
Topic.create(title: "title3", body: "body3")
|
24
26
|
]
|
25
27
|
topics.each { |topic| topic.posts.create(body: "post#{topic.id}") }
|
26
28
|
expired_topic = topics.first
|
@@ -28,8 +30,9 @@ class PreloaderTest < ActiveSupport::TestCase
|
|
28
30
|
|
29
31
|
results = nil
|
30
32
|
assert_queries(2) do
|
31
|
-
assert_sql(/WHERE\s\"topics\"\.\"id\"\s=\s
|
32
|
-
results = Post.includes(:topic).order(
|
33
|
+
assert_sql(/WHERE\s\"topics\"\.\"id\"\s=\s?/m) do
|
34
|
+
results = Post.includes(:topic).order("id ASC").to_a
|
35
|
+
assert_equal expired_topic, results.first.topic
|
33
36
|
end
|
34
37
|
end
|
35
38
|
|