/** * * @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 get_transient() function. * * This method does retrieves the transient with the given transient key. In addition, it checks if it is an array; otherwise, it makes it an array. * * @access public * @since 2.0.0 * @since 3 Changed to use a custom database. * @remark The scope is public as the event method uses it. */ public function getCache($sTransientKey) { if ($this->bUseCacheTable) { $_oCacheTable = new AmazonAutoLinks_DatabaseTable_request_cache(AmazonAutoLinks_Registry::$aDatabaseTables['request_cache']); $_aData = $_oCacheTable->getCache($sTransientKey); $vData = $_aData['data']; } else { $vData = AmazonAutoLinks_WPUtility::getTransient($sTransientKey); } // if it's false, no transient is stored. Otherwise, some values are in there. if (in_array($vData, array(false, ''), true)) { return false; } // If it's array, okay. if (is_array($vData)) { return $vData; } // Maybe it's encoded if (is_string($vData) && is_serialized($vData)) { return unserialize($vData); } // Maybe it's an object. In that case, convert it to an associative array. if (is_object($vData)) { return get_object_vars($vData); } // It's an unknown type, then cast array. return (array) $vData; }
/** * * @return object|array Response array or WP Error object. */ protected function _getHTTPResponseWithCache(array $aURLs, $aArguments = array(), $iCacheDuration = 86400) { $_aData = array(); $_aValidCaches = array(); // First retrieve the cache $_oCacheTable = new AmazonAutoLinks_DatabaseTable_request_cache(AmazonAutoLinks_Registry::$aDatabaseTables['request_cache']); // If a cache exists, use it. $_aCaches = 0 === $iCacheDuration ? array() : $_oCacheTable->getCache(array_keys($aURLs), $iCacheDuration); foreach ($_aCaches as $_aCache) { // Format $_aCache = $_aCache + array('remained_time' => 0, 'charset' => null, 'data' => null, 'request_uri' => null, 'name' => null); if (!isset($_aCache['data'])) { continue; } // Filters - this allows external components to modify the remained time, // which can be used to trick the below check and return the stored data anyway. // So the cache renewal event can be scheduled in the background. $_aCache = apply_filters('aal_filter_http_response_cache', $_aCache, $iCacheDuration, $aArguments, $this->sRequestType); // Set a valid item. if ($_aCache['remained_time'] && $_aCache['data']) { $this->sLastCharSet = $_aCache['charset']; $_aValidCaches[$_aCache['request_uri']] = $_aCache['data']; } } // Check if caches exist one by one and if not, get the response and set a cache. foreach ($aURLs as $_sURL) { if (isset($_aValidCaches[$_sURL])) { $_aData[$_sURL] = $_aValidCaches[$_sURL]; continue; } // Perform an HTTP request. $_aData[$_sURL] = $this->_getHTTPResponse($_sURL, $aArguments); $this->_setCache($_sURL, $_aData[$_sURL], $iCacheDuration); } return $_aData; }