|
|
|
|
@ -198,34 +198,19 @@ module CacheConcern
|
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# TODO: Rename this method, as it does not perform any caching anymore. |
|
|
|
|
def cache_collection(raw, klass) |
|
|
|
|
return raw unless klass.respond_to?(:with_includes) |
|
|
|
|
return raw unless klass.respond_to?(:preload_cacheable_associations) |
|
|
|
|
|
|
|
|
|
raw = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation) |
|
|
|
|
return [] if raw.empty? |
|
|
|
|
records = raw.to_a |
|
|
|
|
|
|
|
|
|
cached_keys_with_value = begin |
|
|
|
|
Rails.cache.read_multi(*raw).transform_keys(&:id).transform_values { |r| ActiveRecordCoder.load(r) } |
|
|
|
|
rescue ActiveRecordCoder::Error |
|
|
|
|
{} # The serialization format may have changed, let's pretend it's a cache miss. |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
uncached_ids = raw.map(&:id) - cached_keys_with_value.keys |
|
|
|
|
|
|
|
|
|
klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!) |
|
|
|
|
|
|
|
|
|
unless uncached_ids.empty? |
|
|
|
|
uncached = klass.where(id: uncached_ids).with_includes.index_by(&:id) |
|
|
|
|
|
|
|
|
|
uncached.each_value do |item| |
|
|
|
|
Rails.cache.write(item, ActiveRecordCoder.dump(item)) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
klass.preload_cacheable_associations(records) |
|
|
|
|
|
|
|
|
|
raw.filter_map { |item| cached_keys_with_value[item.id] || uncached[item.id] } |
|
|
|
|
records |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# TODO: Rename this method, as it does not perform any caching anymore. |
|
|
|
|
def cache_collection_paginated_by_id(raw, klass, limit, options) |
|
|
|
|
cache_collection raw.cache_ids.to_a_paginated_by_id(limit, options), klass |
|
|
|
|
cache_collection raw.to_a_paginated_by_id(limit, options), klass |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|