Skip to content

Commit

Permalink
Remove require_if_necessary, since it is no longer necessary (#412)
Browse files Browse the repository at this point in the history
I think this was added to support loading constants that hadn't been
autoloaded yet, back when we marshal dumped the record objects themselves
and before active support patched Marshal.load to do this.

This is no longer necessary, because we cache the attributes before they
are type cast to avoid making them dependent on active record's object
format or any constant names.
  • Loading branch information
dylanahsmith authored Nov 20, 2019
1 parent 7acb986 commit b2b69be
Showing 1 changed file with 26 additions and 48 deletions.
74 changes: 26 additions & 48 deletions lib/identity_cache/query_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,21 @@ def fetch_by_id(id, includes: nil)
id = type_for_attribute(primary_key).cast(id)
return unless id
record = if should_use_cache?
require_if_necessary do
object = nil
coder = IdentityCache.fetch(rails_cache_key(id)) do
Encoder.encode(object = resolve_cache_miss(id))
end
object ||= Encoder.decode(coder, self)
if object && object.id != id
IdentityCache.logger.error(
<<~MSG.squish
[IDC id mismatch] fetch_by_id_requested=#{id}
fetch_by_id_got=#{object.id}
for #{object.inspect[(0..100)]}
MSG
)
end
object
object = nil
coder = IdentityCache.fetch(rails_cache_key(id)) do
Encoder.encode(object = resolve_cache_miss(id))
end
object ||= Encoder.decode(coder, self)
if object && object.id != id
IdentityCache.logger.error(
<<~MSG.squish
[IDC id mismatch] fetch_by_id_requested=#{id}
fetch_by_id_got=#{object.id}
for #{object.inspect[(0..100)]}
MSG
)
end
object
else
resolve_cache_miss(id)
end
Expand All @@ -69,21 +67,19 @@ def fetch_multi(*ids, includes: nil)
id_type = type_for_attribute(primary_key)
ids.map! { |id| id_type.cast(id) }.compact!
records = if should_use_cache?
require_if_necessary do
cache_keys = ids.map { |id| rails_cache_key(id) }
key_to_id_map = Hash[ cache_keys.zip(ids) ]
key_to_record_map = {}

coders_by_key = IdentityCache.fetch_multi(cache_keys) do |unresolved_keys|
ids = unresolved_keys.map { |key| key_to_id_map[key] }
records = find_batch(ids)
key_to_record_map = records.compact.index_by { |record| rails_cache_key(record.id) }
records.map { |record| Encoder.encode(record) }
end
cache_keys = ids.map { |id| rails_cache_key(id) }
key_to_id_map = Hash[ cache_keys.zip(ids) ]
key_to_record_map = {}

coders_by_key = IdentityCache.fetch_multi(cache_keys) do |unresolved_keys|
ids = unresolved_keys.map { |key| key_to_id_map[key] }
records = find_batch(ids)
key_to_record_map = records.compact.index_by { |record| rails_cache_key(record.id) }
records.map { |record| Encoder.encode(record) }
end

cache_keys.map do |key|
key_to_record_map[key] || Encoder.decode(coders_by_key[key], self)
end
cache_keys.map do |key|
key_to_record_map[key] || Encoder.decode(coders_by_key[key], self)
end
else
find_batch(ids)
Expand Down Expand Up @@ -130,24 +126,6 @@ def check_association_scope(association_name)
end
end

def require_if_necessary #:nodoc:
# mem_cache_store returns raw value if unmarshal fails
rval = yield
case rval
when String
rval = Marshal.load(rval)
when Array
rval.map! { |v| v.kind_of?(String) ? Marshal.load(v) : v }
end
rval
rescue ArgumentError => e
if e.message =~ /undefined [\w\/]+ (\w+)/
ok = Kernel.const_get($1) rescue nil
retry if ok
end
raise
end

def resolve_cache_miss(id)
record = includes(cache_fetch_includes).where(primary_key => id).take
setup_embedded_associations_on_miss([record]) if record
Expand Down

0 comments on commit b2b69be

Please sign in to comment.