public static function doReadQuery($sql, $cacheTime = 60) { $cacheExists = class_exists('PhpCache'); $logExists = class_exists('Log'); $callingFunc = self::getCallingFunction(); $sql = "-- {$callingFunc};\n" . $sql; if ($cacheExists) { $cache = new PhpCache($sql, $cacheTime); if ($cacheTime && $cache->check()) { $cached = $cache->get(); return $cached['data']; } } $conn = SQL::getReadConnection(); if (self::CONNECTION_ERROR == $conn) { return null; } $startTime = microtime(true); $result = mysql_query($sql, $conn); $endTime = microtime(true); $totalTime = $endTime - $startTime; if ($logExists && $totalTime > 1) { Log::logSQL(SQL::getCallingFunction(), $endTime - $startTime, $sql); } if ($logExists && (!$result || mysql_error($conn))) { Log::error("SQL performed: {$sql}\n\nError: " . mysql_error($conn)); return null; } $rows = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { array_push($rows, $row); } if ($cacheExists) { $cached = array(); $cached['function'] = $callingFunc; $cached['data'] = $rows; $cache->set($cached); } return $rows; }
public function getFromPhpSourceAsPost($url, $aOptions = array()) { $cacheTime = isset($aOptions['cache-time']) ? $aOptions['cache-time'] : 60 * 60 * 24 * 30; // 30 Days default $postFields = isset($aOptions['post-fields']) ? $aOptions['post-fields'] : ''; $cacheIdent = isset($aOptions['cache-ident']) ? $aOptions['cache-ident'] : ''; $cache = new PhpCache($url . '?' . $postFields, $cacheTime, $cacheIdent); if ($cache->check()) { $result = $cache->get(); $result = $result['data']; } else { $session = curl_init(); curl_setopt($session, CURLOPT_URL, $url); curl_setopt($session, CURLOPT_HEADER, false); curl_setopt($session, CURLOPT_RETURNTRANSFER, 1); curl_setopt($session, CURLOPT_POST, 1); curl_setopt($session, CURLOPT_POSTFIELDS, $postFields); $result = curl_exec($session); curl_close($session); $result = unserialize($result); $cache->set(array('url' => $url, 'method' => 'getFromPhpSourceAsPost', 'data' => $result)); } return $result; }
private function getMulti($urls, $aOptions = array()) { $results = array(); $mh = curl_multi_init(); $handles = array(); foreach ($urls as $key => $url) { $cacheTime = isset($aOptions[$key]) && isset($aOptions[$key]['cache-time']) ? $aOptions[$key]['cache-time'] : 60 * 60 * 24 * 30; // 30 Days default $type = isset($aOptions[$key]) && isset($aOptions[$key]['type']) ? $aOptions[$key]['type'] : null; $cacheIdent = isset($aOptions[$key]) && isset($aOptions[$key]['cache-ident']) ? $aOptions[$key]['cache-ident'] : ''; $curlOpts = isset($aOptions[$key]) && isset($aOptions[$key]['curlopts']) ? $aOptions[$key]['curlopts'] : null; $cache = new PhpCache($url . serialize($curlOpts), $cacheTime, $cacheIdent); if ($cache->check() && 1 == 0) { $result = $cache->get(); $datatype = $result['datatype']; $result = $result['data']; if ('xml' == $datatype) { $result = simplexml_load_string($result); } $results[$key] = $result; } else { $handles[$key] = curl_init(); // set any headers the user wants if (is_array($curlOpts)) { foreach ($curlOpts as $key => $value) { curl_setopt($session, $key, $value); } } // then set our expected headers curl_setopt($handles[$key], CURLOPT_URL, $url); curl_setopt($handles[$key], CURLOPT_HEADER, false); curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, 1); curl_multi_add_handle($mh, $handles[$key]); } } $active = null; //execute the handles do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active && $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } //close the handles foreach ($handles as $key => $handle) { $result = curl_multi_getcontent($handle); $cacheResult = $result; $cacheTime = isset($aOptions[$key]) && isset($aOptions[$key]['cache-time']) ? $aOptions[$key]['cache-time'] : 60 * 60 * 24 * 30; // 30 Days default $type = isset($aOptions[$key]) && isset($aOptions[$key]['type']) ? $aOptions[$key]['type'] : null; $cacheIdent = isset($aOptions[$key]) && isset($aOptions[$key]['cache-ident']) ? $aOptions[$key]['cache-ident'] : ''; $curlOpts = isset($aOptions[$key]) && isset($aOptions[$key]['curlopts']) ? $aOptions[$key]['curlopts'] : null; $cache = new PhpCache($url . serialize($curlOpts), $cacheTime, $cacheIdent); switch ($type) { case 'json': $result = json_decode($result, true); $cacheResult = $result; $datatype = 'php'; // ya rly break; case 'xml': $result = simplexml_load_string($result); $datatype = 'xml'; break; default: $result = unserialize($result); $cacheResult = $result; $datatype = 'php'; } $cache->set(array('url' => $url, 'method' => 'get', 'datatype' => $datatype, 'data' => $cacheResult)); $results[$key] = $result; curl_multi_remove_handle($mh, $handle); } curl_multi_close($mh); return $results; }