Cache key generation doesn't honor ActiveRecord::Relation objects for `Cache::Store#write`

[`retrieve_cache_key`](https://github.com/rails/rails/blob/ca20037899e55ddf734b727454a3180bebf82212/activesupport/lib/active_support/cache.rb#L91) handles the `to_a` case, but [`expanded_key`](https://github.com/rails/rails/blob/ca20037899e55ddf734b727454a3180bebf82212/activesupport/lib/active_support/cache.rb#L512) currently doesn't. This means if you pass an ActiveRecord::Relation into Rails.cache.write (at any depth), it doesn't properly extract the cache_key, instead just calling `to_param` on each object, which is (by default) just the ID, and therefore the key-based expiration is broken.

This bug [breaks caching in jbuilder](https://github.com/rails/jbuilder/blob/788981134defbde34e94d32eea0c02178bb772a5/lib/jbuilder/jbuilder_template.rb#L53), among other things.

Is there any reason we can't make a few small modification and just call `ActiveSupport::Cache#expand_cache_key` directly instead of duplicating this logic?

This can be backported to 4.0.x, I'll wait for any feedback before doing that.