Fix caching locking issue on Windows

Locking breaks caching on windows, as reported in #22.
Update to use fwrite. To be honest, this locking looks over the
top - I think we could strip it down significantly, so may be
a job for the future.