Why would there be a thick sludge under the oil cap?

The reason why there is a thick sludge under your oil cap is because the engine ventilates crankcase pressure up through the valve covers, this includes oil residue that builds up under the oil cap over an extended period of time. It is good to have your lubrication system flushed out once a year to prevent this oil sludge build up, and also ensure that the vehicle upkeep is maintained such as regular oil changes and air filter changes. Also when you check the oil take the oil fill cap off and wipe it with a rag or cloth from time to time. You should check your oil level at least once a week.

Answer

I agree with Mackman-lots of newer vehicles have oil caps with hollowed out centers that allow moisture and sludge to become trapped within, so that the sludge does not continue to circulate throughout the crankcase. It is a very sensible idea to wipe off the inside of the cap periodically.

Answer

Also a bad PCV valve can cause this. The pcv valve allows byproducts and vapors of combusion to be re-introduced back into the engine for re-combustion. The valve is usually located at or near the oil cap. If it malfunctions or gets clogged over time (most of them do eventually) this can cause the vapors to build up in the oil reservoir and associated compartments. PCV valves are cheap (5-10 bucks) and are easy to replace in most cases.