Fixed an issue where the cache file might be partially written.