Added a flag to decide whether to write image to cache.