Use tmpfile + rename for filebased cache backend

Borrowed some code from django.contrib.sessions.backends.file to make the filebased cache backend less error prone in multiprocess setups.