Skip to content

Commit

Permalink
Merge pull request #103 from OuYangJinTing/Fix-should-fetch-from-db-w…
Browse files Browse the repository at this point in the history
…hen-use-eager-load

Fix: use eager_load/preload should fetch from db
  • Loading branch information
hooopo authored Apr 12, 2020
2 parents e016fa2 + 993aa41 commit edeb6d3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
32 changes: 15 additions & 17 deletions lib/second_level_cache/active_record/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,26 +41,31 @@ 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

private

# readonly_value - active_record/relation/query_methods.rb Rails 5.1 true/false
def cachable?
limit_one? &&
order_values_can_cache? &&
second_level_cache_enabled? &&
limit_one? &&
# !eager_loading? &&
includes_values.blank? &&
preload_values.blank? &&
eager_load_values.blank? &&
select_values.blank? &&
order_values_can_cache? &&
readonly_value.blank? &&
joins_values.blank? &&
!@klass.locking_enabled? &&
Expand Down Expand Up @@ -96,10 +98,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
27 changes: 27 additions & 0 deletions test/finder_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ 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_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
Expand Down

0 comments on commit edeb6d3

Please sign in to comment.