Example #1
0
 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 processConnector($sName)
 {
     $oFactory = PhpCache::getInstance();
     $oCache = $oFactory->create($sName);
     $this->assertInstanceOf('phpCache\\' . $sName, $oCache);
     $oCache->clearAll();
     $aKeys = array();
     $aKeys[0] = new CacheKey('Test1');
     $aKeys[1] = new CacheKey('Test1', 'Prop1');
     $aKeys[2] = new CacheKey($oFactory);
     $aKeys[3] = new CacheKey($oFactory, 'Prop2');
     foreach ($aKeys as $oKey) {
         $this->assertInstanceOf('phpCache\\CacheKey', $oKey);
         $this->assertFalse($oCache->check($oKey));
         $sValue = 'test Value';
         $oCache->set($oKey, $sValue);
         $this->assertTrue($oCache->check($oKey));
         $this->assertEquals($sValue, $oCache->get($oKey));
         $oCache->clear($oKey);
         $this->assertFalse($oCache->check($oKey));
         $this->assertFalse($oCache->get($oKey));
         $aSetValue = array(1 => 2, 'key' => 'This is key');
         $oCache->set($oKey, $aSetValue);
         $aGet = $oCache->get($oKey);
         $this->assertInternalType('array', $aGet);
         $this->assertArrayHasKey(1, $aGet);
         $this->assertEquals(2, $aGet[1]);
         $this->assertArrayHasKey('key', $aGet);
         $this->assertEquals('This is key', $aGet['key']);
         $oCache->clear($oKey);
         $aSetValue = new \stdClass();
         $aSetValue->key1 = 2;
         $aSetValue->key2 = 'This is key';
         $oCache->set($oKey, $aSetValue);
         $aGet = $oCache->get($oKey);
         $this->assertInstanceOf('\\stdClass', $aGet);
         $this->assertEquals(2, $aGet->key1);
         $this->assertEquals('This is key', $aGet->key2);
         $oCache->clear($oKey);
     }
 }
Example #4
0
 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;
 }
 function SimpleRss($sUrl, $vCacheTime = 300, $iNumItems = -1, $sInputEncoding = '', $sOutputEncoding = 'UTF-8')
 {
     $this->oRssObject = new RssObject();
     // this object holds the returned data
     $this->iNumItems = $iNumItems;
     $this->sInputEncoding = $sInputEncoding;
     $this->sOutputEncoding = $sOutputEncoding;
     // make sure caching hasn't been disabled
     if ($vCacheTime) {
         $oCache = new PhpCache($sUrl . '_' . $this->iNumItems, $vCacheTime);
     }
     // is caching disabled or if not has the cache expired
     if (!$vCacheTime || !$oCache->Check()) {
         $oHttp = new Http();
         // request feed
         if ($sData = $oHttp->Get($sUrl)) {
             $this->bSuccessful = $this->Parse($sData);
             // do we want to cache result
             if ($vCacheTime) {
                 $oCache->Set($this->oRssObject);
             }
         }
     } else {
         // get data from cache
         $this->oRssObject = $oCache->Get();
         $this->bSuccessful = true;
         $this->bCached = true;
     }
     // check to see if request was successful, if not try and retrieve stale cache
     if (!$this->bSuccessful && $vCacheTime && $oCache->Exists()) {
         // make cache fresh
         $oCache->ReValidate();
         // get data from cache
         $this->oRssObject = $oCache->Get();
         $this->bSuccessful = true;
         $this->bCached = true;
         // mark as stale request
         $this->bStaleCache = true;
     }
 }