1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

ActiveRecord: Optimize cache_key computation (#41741)

* Optimize cache_key computation

* After review

[Svyatoslav Kryukov + Rafael Mendonça França]
This commit is contained in:
Svyatoslav Kryukov 2021-03-25 05:42:34 +03:00 committed by GitHub
parent 44c3ce2207
commit cb95c1b14f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 1 deletions

View file

@ -357,7 +357,7 @@ module ActiveRecord
def compute_cache_version(timestamp_column) # :nodoc:
timestamp_column = timestamp_column.to_s
if loaded? || distinct_value
if loaded?
size = records.size
if size > 0
timestamp = records.map { |record| record.read_attribute(timestamp_column) }.max
@ -370,6 +370,7 @@ module ActiveRecord
if collection.has_limit_or_offset?
query = collection.select("#{column} AS collection_cache_key_timestamp")
query._select!(table[Arel.star]) if distinct_value && collection.select_values.empty?
subquery_alias = "subquery_for_cache_key"
subquery_column = "#{subquery_alias}.collection_cache_key_timestamp"
arel = query.build_subquery(subquery_alias, select_values % subquery_column)

View file

@ -171,6 +171,7 @@ module ActiveRecord
developers = Developer.distinct.order(:salary).limit(5)
assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\z/, developers.cache_key)
assert_not_predicate developers, :loaded?
end
test "cache_key with a relation having custom select and order" do