Fixed a bug with incorrect cache iteration while flushing.