Esempio n. 1
0
 /**
  * @param $url
  * @param int $cacheTime
  *
  * @return string
  */
 public function getData($url, $cacheTime = 3600)
 {
     // Md5 the url
     $md5 = md5($url);
     // Get results if they are available in the cache already
     $result = $cacheTime > 0 ? $this->cache->get($md5) : null;
     // If there was no result, get the data
     if (!$result) {
         // Init curl
         $curl = curl_init();
         // Setup curl
         curl_setopt_array($curl, array(CURLOPT_USERAGENT => $this->app->baseConfig->getConfig('userAgent', 'site', 'DataGetter from Seriis (karbowiak@gmail.com)'), CURLOPT_TIMEOUT => 30, CURLOPT_POST => false, CURLOPT_FORBID_REUSE => false, CURLOPT_ENCODING => '', CURLOPT_URL => $url, CURLOPT_HTTPHEADER => array('Connection: keep-alive', 'Keep-Alive: timeout=10, max=1000'), CURLOPT_RETURNTRANSFER => true, CURLOPT_FAILONERROR => true));
         // Get the data
         $result = curl_exec($curl);
         // Cache the data
         if ($cacheTime > 0) {
             $this->cache->set($md5, $result, $cacheTime);
         }
     }
     // Return the data
     return $result;
 }
Esempio n. 2
0
 /**
  * @param string $query
  * @param array $parameters
  * @param int $cacheTime
  *
  * @return array|bool
  *
  * @throws Exception
  */
 public function query($query, $parameters = array(), $cacheTime = 30)
 {
     // Sanity check
     if (strpos($query, ';') !== false) {
         throw new Exception('Semicolons are not allowed in queries. Use parameters instead.');
     }
     // Cache time of 0 seconds means skip all caches. and just do the query
     $key = $this->getKey($query, $parameters);
     // If cache time is above 0 seconds, lets try and get it from that.
     if ($cacheTime > 0) {
         // Try the cache system
         $result = !empty($this->cache->get($key)) ? unserialize($this->cache->get($key)) : null;
         if (!empty($result)) {
             return $result;
         }
     }
     try {
         // Start the timer
         $timer = $this->timer;
         // Increment the queryCounter
         $this->queryCount++;
         // Prepare the query
         $stmt = $this->pdo->prepare($query);
         // Execute the query, with the parameters
         $stmt->execute($parameters);
         // Check for errors
         if ($stmt->errorCode() != 0) {
             return false;
         }
         // Fetch an associative array
         $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
         // Close the cursor
         $stmt->closeCursor();
         // Stop the timer
         $duration = $timer->stop();
         // If cache time is above 0 seconds, lets store it in the cache.
         if ($cacheTime > 0) {
             $this->cache->set($key, serialize($result), min(3600, $cacheTime));
         }
         // Store it in the cache system
         // Log the query
         $this->logQuery($query, $parameters, $duration);
         // now to return the result
         return $result;
     } catch (Exception $e) {
         // There was some sort of nasty nasty nasty error..
         throw $e;
     }
 }