Skip to content

Preserve key order of #fetch_multi#34700

Merged
rafaelfranca merged 1 commit intorails:masterfrom
gmcgibbon:fetch_multi_key_order
Jan 7, 2019
Merged

Preserve key order of #fetch_multi#34700
rafaelfranca merged 1 commit intorails:masterfrom
gmcgibbon:fetch_multi_key_order

Conversation

@gmcgibbon
Copy link
Member

Summary

Fixes #34694.

Preserves order of keys passed to #fetch_multi.

If for some reason the current functionality is intended, I'll refactor this PR to document the behaviour.

Copy link
Member

@jeremy jeremy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice @gmcgibbon!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's illuminate this a bit, e.g. fetch_multi(*names) returns its results in the same order as the *names requested, rather than returning cache hits followed by cache misses.

@gmcgibbon gmcgibbon force-pushed the fetch_multi_key_order branch from a8915d3 to 4597418 Compare December 13, 2018 18:29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried grokking this over multiple attempts, but I find the two maps quite confusing since we seem to do similar yet different processing.

What if we kept some of the old flow ala this?

instrument :read_multi, names, options do |payload|
  reads = read_multi_entries(names, options)
  payload[:hits] = reads.keys
  payload[:super_operation] = :fetch_multi

  writes  = {}
  ordered = names.each_with_object({}) do |name, hash|
    hash[name] = reads.fetch(name) { writes[name] = yield name }
  end

  write_multi writes, options
  ordered
end

(Probably ends up needing to be yield(name). Psst, also a nice case for index_with, but we can't use core extensions here.)

fetch_multi(*names) now returns its results in the same order
as the `*names` requested, rather than returning cache hits
followed by cache misses.
@gmcgibbon gmcgibbon force-pushed the fetch_multi_key_order branch from 4597418 to 94ecf95 Compare December 27, 2018 07:41
@rafaelfranca rafaelfranca merged commit cd16413 into rails:master Jan 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants