How to save a value to the cache?

Magento 1.x code

Mage::app()->getCacheInstance()->save($data, $key, $tags, $lifeTime);

Magento 2 code

/**
 * @param string $data
 * @param string $key
 * @param [] $tags [optional]
 * @param int|null|false $lifeTime [optional]
 * @return bool
 */
function df_cache_save($data, $key, $tags = [], $lifeTime = null) {
	/** @var \Magento\Framework\ObjectManagerInterface $om */
	$om = \Magento\Framework\App\ObjectManager::getInstance();
	/** @var \Magento\Framework\App\CacheInterface $cache */
	$cache = $om->get('Magento\Framework\App\CacheInterface');
	return $cache->save($data, $key, $tags, $lifeTime);
}

A post was split to a new topic: How to cache an array?

2 posts were split to a new topic: Whether the df_cache_save() will cache forever without the $lifeTime argument?

See also: