Adding locking when clearing the cache

This avoids conflicts if multiple workers that share a common filesystem
simultaneously attempt to clear the cache.