cache = $cache; $this->cacheState = $cacheState; $this->storeManager = $storeManager; $this->json = $json ?: ObjectManager::getInstance()->get(Json::class); parent::__construct($context); } /** * @param Location $location * @return bool|string * @throws NoSuchEntityException */ public function load(Location $location) { if (!$this->isEnabled()) { return false; } $cacheId = $this->getCacheKey($location->getData()); $_data = $this->cache->load($cacheId); if (!$_data) { return false; } try { $data = $this->json->unserialize($_data); } catch (InvalidArgumentException $exception) { $data = $_data; } return $data; } /** * @return bool */ public function isEnabled() { return $this->cacheState->isEnabled(GoogleMapLocations::TYPE_IDENTIFIER); } /** * @param array $key * @return string * @throws NoSuchEntityException */ public function getCacheKey(array $key) { $key = array_values($key); // ignore array keys $key[] = $this->storeManager->getStore()->getId(); $key = implode('|', $key); $key = sha1($key); // use hashing to hide potentially private data return GoogleMapLocations::CACHE_KEY_PREFIX . $key; } /** * @param array $data * @param Location $location * @param int $cacheLifetime * @return bool * @throws NoSuchEntityException */ public function save($data, Location $location, $cacheLifetime = 0) { if (!$this->isEnabled()) { return false; } $data = $this->json->serialize($data); $cacheId = $this->getCacheKey($location->getData()); return $this->cache->save( $data, $cacheId, [GoogleMapLocations::CACHE_TAG], 0 < $cacheLifetime ? $cacheLifetime : GoogleMapLocations::CACHE_LIFETIME ); } }