Extract caching logic into a class.