David and his soldiers were in the cave because it was safe there, and another group of Philistia soldiers was occupying Bethlehem.