|
|
|
@ -46,27 +46,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 = Rails.cache.read_multi(*raw).transform_keys(&:id)
|
|
|
|
|
klass.preload_cacheable_associations(records)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
Rails.cache.write_multi(uncached.values.to_h { |i| [i, i] })
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|