Check DOING_AUTOSAVE before flushing transients

So that we don’t flush transients more often than necessary.

Fixes #646