Skip to content

Commit

Permalink
Merge pull request #566 from Shopify/change-order-use-idc-check
Browse files Browse the repository at this point in the history
Change order in if clause for should_use_cache?
  • Loading branch information
kirs committed Mar 19, 2024
2 parents 22f3feb + 55ad14c commit 5a705b0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## Unreleased

## 1.5.6

- Minor performance improvements on association read

## 1.5.5

- Minor performance improvements on association read


## 1.5.4

- Make `prefetch_associations` work as expected on associations that have been partially prefetched
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
identity_cache (1.5.5)
identity_cache (1.5.6)
activerecord (>= 7.0)
ar_transaction_changes (~> 1.1)

Expand Down
2 changes: 1 addition & 1 deletion lib/identity_cache/cached/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def build
reflection.active_record.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
def #{cached_accessor_name}
association_klass = association(:#{name}).klass
if (loaded_by_idc? || association_klass.should_use_cache?) && #{reflection.foreign_key}.present? && !association(:#{name}).loaded?
if #{reflection.foreign_key}.present? && !association(:#{name}).loaded? && (loaded_by_idc? || association_klass.should_use_cache?)
if defined?(#{records_variable_name})
#{records_variable_name}
else
Expand Down
2 changes: 1 addition & 1 deletion lib/identity_cache/cached/recursive/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def build
def read(record)
assoc = record.association(name)

if (record.send(:loaded_by_idc?) || assoc.klass.should_use_cache?) && !assoc.loaded? && assoc.target.blank?
if !assoc.loaded? && assoc.target.blank? && (record.send(:loaded_by_idc?) || assoc.klass.should_use_cache?)
if record.instance_variable_defined?(records_variable_name)
record.instance_variable_get(records_variable_name)
elsif record.instance_variable_defined?(dehydrated_variable_name)
Expand Down
2 changes: 1 addition & 1 deletion lib/identity_cache/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module IdentityCache
VERSION = "1.5.5"
VERSION = "1.5.6"
CACHE_VERSION = 8
end
27 changes: 27 additions & 0 deletions test/fetch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,18 @@ def test_returned_records_are_not_readonly_with_open_transactions
end
end

def test_should_use_cache_not_called_on_preloaded
Item.send(:cache_belongs_to, :item)

bob = Item.create!(title: "bob")
john = Item.create!(title: "john")
bob.update_column(:item_id, john.id)

from_db = Item.includes(:item).find(bob.id)
IdentityCache.expects(:should_use_cache?).never
from_db.fetch_item
end

def test_should_use_cache_not_called_on_prefetched
Item.send(:cache_belongs_to, :item)

Expand All @@ -366,6 +378,21 @@ def test_should_use_cache_not_called_on_prefetched_multi
items.first.fetch_item
end

def test_should_use_cache_not_called_on_preloaded_has_many
Item.send(:cache_has_many, :associated_records, embed: true)

bob = Item.create!(title: "bob")

bob.associated_records.create!(name: "foo")
bob.associated_records.create!(name: "bar")

from_db = Item.includes(:associated_records).find(bob.id)

IdentityCache.expects(:should_use_cache?).never
records = from_db.fetch_associated_records
assert_equal(2, records.size)
end

def test_should_use_cache_not_called_on_embedded_has_many
Item.send(:cache_has_many, :associated_records, embed: true)

Expand Down

0 comments on commit 5a705b0

Please sign in to comment.