The title for this Psalm is, "A maskil of David. When he was in the cave. A prayer." The word "maskil" is an untranslated word which has the idea that David had these thoughts and wrote them down. He also understood that God inspired the words of this Psalm.
One may ask why a cave is mentioned in this heading. In the books of 1 and 2 Samuel we see various references to David hiding in caves. The idea of hiding in a cave insinuates that one is scared to come out into the open. He is probably fearful for his very life. The point of this Psalm is that whatever David was going through, he realized only God could rescue him from the darkness he was facing at this moment.
From this short Psalm, we too can learn how to trust God through the dark times in our lives.
In verse1, David cries unto the LORD for mercy. We get the impression that whatever pain David was dealing with at this moment, he realized God Himself provides the only solution to his problems. David realized that as long as God exists and desires to guide his life, it is never over until God says it is over.
Notice David is using the most holy name of God. The word "LORD" in all capitals means in effect, "I am that I am". My point is that a perfect God knows all things and cares about what happens to our lives. He is perfectly capable of rescuing us out of whatever "cave" we are in at the moment.
Why is David asking for mercy here? Did David do something wrong to get in the cave in the first place? I don't believe that is the point. David is asking God for help, but at the same time David realizes he is not perfect. He desires God's mercy which brings freedom. Even in our own lives, It may be God's will for us to remain in a particular situation for a longer period of time in order to teach us more things. It is not always His will to end our "cave" experience. The glaring lesson in these thoughts is all about true surrender. We have to let go of our will in order to deal with whatever darkness we face in that "cave". If we belong to God, then we trust in Him and His timing to end the darkness of the "cave" we face.
Verse 2 continues, I poured out my complaint before Him; I shewed before Him my trouble. David acknowledges that the solution to his dark times in the cave is God. One may ask: If God knows all things, isn't He already aware of the "cave" we are in? Why bother to tell God our problems if He already knows all things? The point is we don't do it for His sake but for ours. Sometimes we can't listen to what He desires for us, until we let it out of our system and lay it at His feet.
Remember in the last Psalm how David wanted God to silence him so he could listen to God? Often we have to speak our peace so we can then listen. Sometimes it is good to describe our situation to God so we can let go of what we have to say in order for God to respond to our requests.