/** * * @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; }
/** * Deletes the cache of the provided URL. */ public function deleteCache() { $_oCacheTable = new AmazonAutoLinks_DatabaseTable_request_cache(AmazonAutoLinks_Registry::$aDatabaseTables['request_cache']); foreach ($this->aURLs as $_sCacheName => $_sURL) { // @todo implement deleteCaches() method $_oCacheTable->deleteCache($_sCacheName); } }
/** * Clears expired caches. * @since 3 * @return void */ private function _clearExpiredCaches($oFactory) { $_oCacheTable = new AmazonAutoLinks_DatabaseTable_request_cache(AmazonAutoLinks_Registry::$aDatabaseTables['request_cache']); $_oCacheTable->deleteExpired(); $_oProductTable = new AmazonAutoLinks_DatabaseTable_product(AmazonAutoLinks_Registry::$aDatabaseTables['product']); $_oProductTable->deleteExpired(); // DELETE FROM table WHERE (col1,col2) IN ((1,2),(3,4),(5,6)) $oFactory->setSettingNotice(__('Caches have been cleared.', 'amazon-auto-links')); }