Cache file stats to avoid IDB lookups (rev2)

These are the rebased and updated patches for #521 and #518. Obsoletes #531.