/** * * @return object|array Response array or WP Error object. */ protected function _getHTTPResponseWithCache($sURL, $aArguments = array(), $iCacheDuration = 86400) { $_oCacheTable = new AmazonAutoLinks_DatabaseTable_request_cache(AmazonAutoLinks_Registry::$aDatabaseTables['request_cache']); // If a cache exists, use it. $_aData = 0 === $iCacheDuration ? array() : $_oCacheTable->getCache($this->sCacheName); $_aData = $_aData + array('remained_time' => 0, 'charset' => null, 'data' => null); if ($_aData['remained_time'] && $_aData['data']) { $this->sLastCharSet = $_aData['charset']; return $_aData['data']; } // @todo maybe implement a mechanism that fetches data in the background // and return the stored data anyway. // Otherwise, retrieve a data from a remote server and set a cache. $_asResponse = file_get_contents($sURL); $this->sLastCharSet = $this->getCharacterSetFromResponseHeader($http_response_header); $_oCacheTable->setCache($_sCacheName, $_asResponse, (int) $iCacheDuration, array('request_uri' => $sURL, 'type' => 'file_get_contents', 'charset' => $this->sLastCharSet)); return $_asResponse; }
/** * A wrapper method for the set_transient() function. * * @since unknown * @since 3 Changed the name from `setTransient()`. */ public function setCache($sTransientKey, $vData, $iDuration = 0, $sRequestURI = '') { $sLockTransient = AmazonAutoLinks_Registry::TRANSIENT_PREFIX . '_' . md5("Lock_{$sTransientKey}"); // Check if the transient is locked if (AmazonAutoLinks_WPUtility::getTransient($sLockTransient) !== false) { return; // it means the cache is being modified right now in a different process. } // Set a lock flag transient that indicates the transient is being renewed. AmazonAutoLinks_WPUtility::setTransient($sLockTransient, time(), AmazonAutoLinks_WPUtility::getAllowedMaxExecutionTime(30, 30)); // Save the cache if ($this->bUseCacheTable) { $_oCacheTable = new AmazonAutoLinks_DatabaseTable_request_cache(AmazonAutoLinks_Registry::$aDatabaseTables['request_cache']); $_aData = $_oCacheTable->setCache($sTransientKey, array('mod' => time(), 'data' => $vData), (int) $iDuration, array('request_uri' => $sRequestURI, 'type' => 'api')); } else { AmazonAutoLinks_WPUtility::setTransient($sTransientKey, array('mod' => time(), 'data' => $this->oEncrypt->encode($vData)), 9999999); } }
/** * Sets a cache by url. * It internally sets a cache name. * @return boolean * @todo Examine the return value as it is not tested. */ private function _setCache($sURL, $mData, $iCacheDuration = 86400) { $_sCharSet = $this->_getCharacterSet($mData); $_oCacheTable = new AmazonAutoLinks_DatabaseTable_request_cache(AmazonAutoLinks_Registry::$aDatabaseTables['request_cache']); $_bResult = $_oCacheTable->setCache($this->_getCacheName($sURL), $mData, $iCacheDuration ? (int) $iCacheDuration : 86400, array('request_uri' => $sURL, 'type' => $this->sRequestType, 'charset' => $_sCharSet)); $this->sLastCharSet = $_sCharSet; return $_bResult; }