Avoid corrupt cache by writing it to a temporary file first, and then overwriting the original one. Should also fix other exceptional cases. Thanks-to: Alexander Strasser <eclipse7@gmx.net>