Lock the cache file while writing to prevent partial reads

I think the change is pretty self-explanatory.  I've been running this in a production environment for a couple weeks now, so I have no reason to doubt stability.  (Prior to this change, the cache would often have blank data.)