Move caching of requirements to helper function and add unit tests.

I'm looking at handling multiple pip requirements files, but don't want to do it without testing.

Having said that, I don't have all that much time to devote to this, so I'm submitting small PRs as I go along so I don't end up with a hugely divergent branch.