From 7b6aea722747f7de08c6623328eb78e58ed10f1b Mon Sep 17 00:00:00 2001 From: OuYangJinTing <2729877005qq@gmail.com> Date: Sun, 12 Apr 2020 13:21:07 +0800 Subject: [PATCH 1/2] Fix: use eager_load should fetch from db --- .../active_record/finder_methods.rb | 31 ++++++++----------- test/finder_methods_test.rb | 9 ++++++ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/second_level_cache/active_record/finder_methods.rb b/lib/second_level_cache/active_record/finder_methods.rb index 18c9531..3b1bc3f 100644 --- a/lib/second_level_cache/active_record/finder_methods.rb +++ b/lib/second_level_cache/active_record/finder_methods.rb @@ -17,16 +17,13 @@ module FinderMethods # Article.where("articles.user_id = 1").find(prams[:id]) # Article.where("user_id = 1 AND ...").find(params[:id]) def find_one(id) - return super(id) unless second_level_cache_enabled? - return super(id) unless select_all_column? + return super unless cachable? id = id.id if ActiveRecord::Base == id - if cachable? - record = @klass.read_second_level_cache(id) - return record if record && where_values_match_cache?(record) - end + record = @klass.read_second_level_cache(id) + return record if record && where_values_match_cache?(record) - record = super(id) + record = super record.write_second_level_cache record end @@ -44,15 +41,16 @@ def find_one(id) # User.where(age: 18).first # def first(limit = nil) - return super(limit) if limit.to_i > 1 + return super if limit.to_i > 1 + return super unless cachable? # only have primary_key condition in where - if cachable? && where_values_hash.length == 1 && where_values_hash.key?(primary_key) + if where_values_hash.length == 1 && where_values_hash.key?(primary_key) record = @klass.read_second_level_cache(where_values_hash[primary_key]) return record if record end - record = super(limit) - record&.write_second_level_cache if select_all_column? + record = super + record&.write_second_level_cache record end @@ -60,10 +58,11 @@ def first(limit = nil) # readonly_value - active_record/relation/query_methods.rb Rails 5.1 true/false def cachable? - limit_one? && + second_level_cache_enabled? && + limit_one? && + !eager_loading? && + select_values.blank? && order_values_can_cache? && - includes_values.blank? && - preload_values.blank? && readonly_value.blank? && joins_values.blank? && !@klass.locking_enabled? && @@ -96,10 +95,6 @@ def where_values_match_cache?(record) def limit_one? limit_value.blank? || limit_value == 1 end - - def select_all_column? - select_values.blank? - end end end end diff --git a/test/finder_methods_test.rb b/test/finder_methods_test.rb index 2f0e452..36f9191 100644 --- a/test/finder_methods_test.rb +++ b/test/finder_methods_test.rb @@ -71,6 +71,15 @@ def test_should_fetch_from_db_if_where_use_string end end + def test_should_fetch_from_db_when_use_eager_load + @user.write_second_level_cache + assert_queries(:any) do + assert_sql(/LEFT\sOUTER\sJOIN\s\"books\"/m) do + User.eager_load(:books).find(@user.id) + end + end + end + def test_where_and_first_should_with_cache @user.write_second_level_cache assert_no_queries do From 993aa417fc033b19d6b3df4a5092bc1a5538dd10 Mon Sep 17 00:00:00 2001 From: OuYangJinTing <2729877005qq@gmail.com> Date: Sun, 12 Apr 2020 23:25:26 +0800 Subject: [PATCH 2/2] Fix: use includes/preload should fetch from db --- .../active_record/finder_methods.rb | 5 ++++- test/finder_methods_test.rb | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/second_level_cache/active_record/finder_methods.rb b/lib/second_level_cache/active_record/finder_methods.rb index 3b1bc3f..8a77a5b 100644 --- a/lib/second_level_cache/active_record/finder_methods.rb +++ b/lib/second_level_cache/active_record/finder_methods.rb @@ -60,7 +60,10 @@ def first(limit = nil) def cachable? second_level_cache_enabled? && limit_one? && - !eager_loading? && + # !eager_loading? && + includes_values.blank? && + preload_values.blank? && + eager_load_values.blank? && select_values.blank? && order_values_can_cache? && readonly_value.blank? && diff --git a/test/finder_methods_test.rb b/test/finder_methods_test.rb index 36f9191..6480cab 100644 --- a/test/finder_methods_test.rb +++ b/test/finder_methods_test.rb @@ -80,6 +80,24 @@ def test_should_fetch_from_db_when_use_eager_load end end + def test_should_fetch_from_db_when_use_includes + @user.write_second_level_cache + assert_queries(:any) do + assert_sql(/SELECT\s\"books\"\.\*\sFROM\s\"books\"/m) do + User.includes(:books).find(@user.id) + end + end + end + + def test_should_fetch_from_db_when_use_preload + @user.write_second_level_cache + assert_queries(:any) do + assert_sql(/SELECT\s\"books\"\.\*\sFROM\s\"books\"/m) do + User.preload(:books).find(@user.id) + end + end + end + def test_where_and_first_should_with_cache @user.write_second_level_cache assert_no_queries do