The cache is a correctness tool
The cache is not only a speed trick. It is a proof that a layer's inputs did not change. If you want a layer to rebuild, change its inputs; if you want it to stay cached, do not touch them.
When the cache lies
A RUN curl https://x line is cached on the command text, not on the fetched content. The fetch can be stale while the hash matches. Bust it with a build arg you pass in, or fetch at runtime.