I simulate a second run on the same key that hits the cache and skips the install, then a run after a change that produces a new key and misses.
requirements = "flask==2.0\nrequests==2.28\n"
def hashfile(name, contents):
return hashlib.sha256(contents.encode()).hexdigest()[:8]
key = "pip-" + hashfile("requirements.txt", requirements)
print("Cache hit -> pip skipped (3s)")
new_requirements = requirements + "pytest==7.0\n"
new_key = "pip-" + hashfile("requirements.txt", new_requirements)
print("After change: key=%s -> miss -> pip install (2m0s)" % new_key)
Before you run it, say out loud what it will print. Then open the answer.
What it prints
Cache hit -> pip skipped (3s)
After change: key=pip-62d36509 -> miss -> pip install (2m0s)
An exact key reuse skips everything, but a changed lock file creates a new key and rebuilds. A cache that never refreshes ships stale packages.