示例#1
0
文件: DB.php 项目: kaibosh/nZEDb
 /**
  * Returns an array of result (empty array if no results or an error occurs)
  * Optional: Pass true to cache the result with a cache server.
  *
  * @param string $query       SQL to execute.
  * @param bool   $cache       Indicates if the query result should be cached.
  * @param int    $cacheExpiry The time in seconds before deleting the query result from the cache server.
  *
  * @return array Array of results (possibly empty) on success, empty array on failure.
  */
 public function query($query, $cache = false, $cacheExpiry = 600)
 {
     if (!$this->parseQuery($query)) {
         return false;
     }
     if ($cache === true && $this->cacheEnabled === true) {
         try {
             $data = $this->cacheServer->get($this->cacheServer->createKey($query));
             if ($data !== false) {
                 return $data;
             }
         } catch (CacheException $error) {
             $this->echoError($error->getMessage(), 'query', 4);
         }
     }
     $result = $this->queryArray($query);
     if ($result !== false && $cache === true && $this->cacheEnabled === true) {
         $this->cacheServer->set($this->cacheServer->createKey($query), $result, $cacheExpiry);
     }
     return $result === false ? [] : $result;
 }
示例#2
0
文件: DB.php 项目: zetas/nZEDb
 /**
  * Returns a multidimensional array of result of the query function return and the count of found rows
  * Note: Query passed to this function SHOULD include SQL_CALC_FOUND_ROWS
  * Optional: Pass true to cache the result with a cache server.
  *
  * @param string $query       SQL to execute.
  * @param bool   $cache       Indicates if the query result should be cached.
  * @param int    $cacheExpiry The time in seconds before deleting the query result from the cache server.
  *
  * @return array Array of results (possibly empty) on success, empty array on failure.
  */
 public function queryCalc($query, $cache = false, $cacheExpiry = 600)
 {
     $data = $this->query($query, $cache, $cacheExpiry);
     if (strpos($query, 'SQL_CALC_FOUND_ROWS') === false) {
         return $data;
     }
     if ($cache === true && $this->cacheEnabled === true) {
         try {
             $count = $this->cacheServer->get($this->cacheServer->createKey($query . 'count'));
             if ($count !== false) {
                 return ['total' => $count, 'result' => $data];
             }
         } catch (CacheException $error) {
             $this->echoError($error->getMessage(), 'queryCalc', 4);
         }
     }
     $result = $this->queryOneRow('SELECT FOUND_ROWS() AS total');
     if ($result !== false && $cache === true && $this->cacheEnabled === true) {
         $this->cacheServer->set($this->cacheServer->createKey($query . 'count'), $result['total'], $cacheExpiry);
     }
     return ['total' => $result === false ? 0 : $result['total'], 'result' => $data];
 }
示例#3
0
文件: DB.php 项目: Jay204/nZEDb
 /**
  * Returns an array of result (empty array if no results or an error occurs)
  * Optional: Pass true to cache the result with a cache server.
  *
  * @param string $query       SQL to execute.
  * @param bool   $cache       Indicates if the query result should be cached.
  * @param int    $cacheExpiry The time in seconds before deleting the query result from the cache server.
  *
  * @return array Array of results (possibly empty) on success, empty array on failure.
  */
 public function query($query, $cache = false, $cacheExpiry = 600)
 {
     if (empty($query)) {
         return false;
     }
     if (nZEDb_QUERY_STRIP_WHITESPACE) {
         $query = Utility::collapseWhiteSpace($query);
     }
     if ($cache === true && $this->cacheEnabled === true) {
         try {
             $data = $this->cacheServer->get($this->cacheServer->createKey($query));
             if ($data !== false) {
                 return $data;
             }
         } catch (CacheException $error) {
             $this->echoError($error->getMessage(), 'query', 4);
         }
     }
     $result = $this->queryArray($query);
     if ($result !== false && $cache === true && $this->cacheEnabled === true) {
         $this->cacheServer->set($this->cacheServer->createKey($query), $result, $cacheExpiry);
     }
     return $result === false ? [] : $result;
 }
示例#4
0
<?php

// Test the cache server connection.
require_once realpath(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'indexer.php');
use nzedb\libraries\Cache;
try {
    $cache = new Cache();
} catch (\Exception $error) {
    exit($error->getMessage() . PHP_EOL);
}
print_r($cache->serverStatistics());