Refactored caching into cache_image_data()