He may well have composed Psalm 142 in one of those caves. If that was the case, King Saul was the one hounding David, seeking to take his life.
فَقَدِ ٱحْتَمَى دَاوُدُ بِمَغَارَتَيْنِ عَلَى ٱلْأَقَلِّ ،‏ إِحْدَاهُمَا بِٱلْقُرْبِ مِنْ عَدُلَّامَ وَٱلْأُخْرَى فِي بَرِّيَّةِ عَيْنَ جَدْيَ .‏