It takes awhile for that to happen,usually during storage between seasons - usually more than one.
There is a preventative measure that can be taken at the end of a season. Rinse the entire cap in hot water and thoroughly dry it. Before replacing it, clean the bottle threads and put a smear of Vaseline on them. Put the cap back on the bottle tightly.
I keep a tuna fish can half filled with paraffin (old candle wax.) Warm it to melting, and dunk the cap in the wax to seal it. Store the bottle upside down.
Over the years, I have theorized that the rust has most of its origins from inside the bottle, (not the from the outside if stored in a dry environment) - oxidation entering through an unsealed cap, combined with vapors from the contents creating a corrosive condensate on the underside of the cap during upright storage. If the wax-sealed bottle is stored upside down, there is no opportunity for that chemistry to manifest itself to cause rusting.
Then, of course, without sounding smart or condescending, if at all possible, the simplest solution is to replace the metal cap with a plastic one. If that is not an option, use the method described above for the metal caps.
Regards,
Jonathan