Improved IOManager.write_cache().

Method now avoids partial writes of cache.

This prevents cache corruption in the case that cache data do not compute
correctly.