Create destination file and then copy into cache instead of the opposite

This simplifies the code so that there is no special case kicking in when
using --serialize-diagnostics (which requires the "-o" option to refer to
the destination instead of a temporary file in the cache).
Refactor code for putting/getting files to/from the cache into functions