Fix race condition in cache

It was previously possible that this code would look up a value in the
cache atom, find it present, and try to return it, only to find that the
value had been removed in between. Instead, it's necessary to do all the
cache accesses atomically.
Merge pull request #8 from leon-barrett/fix-cache

Fix race condition in cache