Write to a temp file and then rename instead of writing directly to a given file
Added locking and per-table cache exclusion