Add locking primitives to data consistency

Haven't really tested this change yet, but it seems to me that we are at a risk of data corruption unless we add these locking primitives. If I understand correctly, just one instance of the cache is shared among all Go routines and hence you could get in a state where there are two simultaneous writes with the same key or a read/write at the same time before the data could be written to disk.