cache = $cache; $this->cacheState = $cacheState; $this->storeManager = $storeManager; $this->storeId = $storeManager->getStore()->getId(); $this->json = $json ?: ObjectManager::getInstance()->get(Json::class); parent::__construct($context); } /** * @param string $method * @param array $vars * @return string */ public function getId($method, $vars = array()) { return base64_encode($this->storeId . static::CACHE_ID . $method . implode('#', (array)$vars)); } /** * @param string $cacheId * @return bool|object */ public function loadObject($cacheId) { $result = $this->load($cacheId); if (!empty($result)) { try { $result = $this->json->unserialize($result); } catch (InvalidArgumentException $e) { return false; } return $result; } return false; } /** * @param string $cacheId * @return bool|string */ public function load($cacheId) { if ($this->cacheState->isEnabled(static::CACHE_ID)) { return $this->cache->load($cacheId); } return false; } /** * @param string $data * @param string $cacheId * @param int $cacheLifetime * @return bool */ public function save($data, $cacheId, $cacheLifetime = 0) { if (!is_string($data)) { $data = $this->json->serialize($data); } if ($this->cacheState->isEnabled(static::CACHE_ID)) { $this->cache->save( $data, $cacheId, [static::CACHE_TAG], empty($cacheLifetime) ? static::CACHE_LIFETIME : $cacheLifetime ); return true; } return false; } }