Factor out the caching helper into its own module.

This will be a common pattern so it's worth having a utility method for
it.