php专区

 首页 > php专区 > PHP应用 > php类库 > php缓存数据功能的mysqli类 - php类库

php缓存数据功能的mysqli类 - php类库

分享到:
【字体:
导读:
          ?php/***Mysqli类**@author废墟*@versionv1.02009-08-18*/classdb_mysqli{protected$mysqli;protected$sql;protected$rs;protected$query_......

php缓存数据功能的mysqli类

  1.     /**   
  2.     * Mysqli类   
  3.     *   
  4.     * @author 废墟   
  5.     * @version v1.0 2009-08-18  
  6.     */    
  7.     class db_mysqli {    
  8.         protected $mysqli;    
  9.         protected $sql;    
  10.         protected $rs;    
  11.         protected $query_num    = 0;    
  12.         protected $fetch_mode    = MYSQLI_ASSOC;    
  13.         protected $cache_dir    = './cache/';    
  14.         protected $cache_time    = 1800;    
  15.   
  16.         public function  __construct($dbhost$dbuser$dbpass$dbname) {    
  17.             $this->mysqli    = new mysqli($dbhost$dbuser$dbpass$dbname);    
  18.             if(mysqli_connect_errno()) {    
  19.                 $this->mysqli    = false;    
  20.                 echo '

    '.mysqli_connect_error().'

    '
    ;    
  21.                 die();    
  22.             } else {    
  23.                 $this->mysqli->set_charset("utf8");    
  24.             }    
  25.         }    
  26.   
  27.         public function  __destruct() {    
  28.             $this->free();    
  29.             $this->close();    
  30.         }    
  31.   
  32.         protected function free() {    
  33.             @$this->rs->free();    
  34.         }    
  35.   
  36.         protected function close() {    
  37.             $this->mysqli->close();    
  38.         }    
  39.   
  40.         protected function fetch() {    
  41.             return $this->rs->fetch_array($this->fetch_mode);    
  42.         }    
  43.   
  44.         protected function getQuerySql($sql$limit = null) {    
  45.             if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is"$limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is"$sql)) {    
  46.                 $sql .= " LIMIT " . $limit;    
  47.             }    
  48.             return $sql;    
  49.         }    
  50.   
  51.         protected function get_cache($sql,$method) {    
  52.             include_once './cache.php';//若框架中使用__autoload(),这里可以不用加载文件    
  53.             $cache    = new cache($this->cache_dir,$this->cache_time);    
  54.             $cache_file    = md5($sql.$method);    
  55.             $res    = $cache->get_cache($cache_file);    
  56.             if(!$res) {    
  57.                 $res    = $this->$method($sql);    
  58.                 $cache->set_cache($cache_file$res);    
  59.             }    
  60.             return $res;    
  61.         }    
  62.   
  63.         public function query_num() {    
  64.             return $this->query_num;    
  65.         }    
  66.   
  67.         public function set_cache_dir($cache_dir) {    
  68.             $this->cache_dir    = $cache_dir;    
  69.         }    
  70.   
  71.         public function set_cache_time($cache_time) {    
  72.             $this->cache_time    = $cache_time;    
  73.         }    
  74.   
  75.         public function query($sql$limit = null) {    
  76.             $sql    = $this->getQuerySql($sql$limit);    
  77.             $this->sql    = $sql;    
  78.             $this->rs    = $this->mysqli->query($sql);    
  79.             if (!$this->rs) {    
  80.                 echo "

    ".$this->mysqli->error."

    "
    ;    
  81.                 die();    
  82.             } else {    
  83.             $this->query_num++;    
  84.                 return $this->rs;    
  85.             }    
  86.         }    
  87.   
  88.         public function getOne($sql) {    
  89.             $this->query($sql, 1);    
  90.             $this->fetch_mode    = MYSQLI_NUM;    
  91.             $row = $this->fetch();    
  92.             $this->free();    
  93.             return $row[0];    
  94.         }    
  95.   
  96.         public function get_one($sql) {    
  97.             return $this->getOne($sql);    
  98.         }    
  99.   
  100.         public function cache_one($sql) {    
  101.             $sql    = $this->getQuerySql($sql, 1);    
  102.             return $this->get_cache($sql'getOne');    
  103.         }    
  104.   
  105.         public function getRow($sql$fetch_mode = MYSQLI_ASSOC) {    
  106.             $this->query($sql, 1);    
  107.             $this->fetch_mode    = $fetch_mode;    
  108.             $row = $this->fetch();    
  109.             $this->free();    
  110.             return $row;    
  111.         }    
  112.   
  113.         public function get_row($sql$fetch_mode = MYSQLI_ASSOC) {    
  114.             return $this->getRow($sql);   
  115.         }    
  116.   
  117.         public function cache_row($sql) {    
  118.             $sql    = $this->getQuerySql($sql, 1);    
  119.             return $this->get_cache($sql'getRow');    
  120.         }    
  121.   
  122.         public function getAll($sql$limit = null, $fetch_mode = MYSQLI_ASSOC) {    
  123.             $this->query($sql$limit);    
  124.             $all_rows = array();    
  125.             $this->fetch_mode    = $fetch_mode;    
  126.             while($rows = $this->fetch()) {    
  127.                 $all_rows[] = $rows;    
  128.             }    
  129.             $this->free();    
  130.             return $all_rows;    
  131.         }    
  132.         public function get_all($sql$limit = null, $fetch_mode = MYSQLI_ASSOC) {    
  133.             return $this->getAll($sql);    
  134.         }    
  135.   
  136.         public function cache_all($sql$limit = null) {    
  137.             $sql    = $this->getQuerySql($sql$limit);    
  138.             return $this->get_cache($sql'getAll');    
  139.         }    
  140.   
  141.         public function insert_id() {    
  142.             return $this->mysqli->insert_id();    
  143.         }    
  144.   
  145.         public function escape($str) {    
  146.             if(is_array($str)) {    
  147.                 foreach($str as $key=>$val) {    
  148.                     $str[$key] = $this->escape($val);    
  149.                 }    
  150.             } else {    
  151.                 $str = addslashes(trim($str));    
  152.             }   //开源代码phpfensi.com 
  153.             return $str;    
  154.         }    
  155.     }    
  156.     //用法    
  157.     $db    = new db_mysqli('localhost''root', 111222, 'dict');    
  158.     $db->set_cache_time(10);    
  159.     $db->set_cache_dir('./cache/sql/');    
  160.     $sql = "select * from words order by word_id limit 10,10";    
  161.     $res1 = $db->get_all($sql);    
  162.     $res2 = $db->cache_all($sql);    
  163.   
  164.     echo $db->query_num(),'
    '
    ;    
  165.     ?>
分享到:
PHP中Memcache操作类使用方法 - php类库
PHP中Memcache操作类使用方法
PHP mysql数据库操作类 - php类库
PHP mysql数据库操作类 数据库操作类是所有程序都可以实现的一个功能,下面我来分享一个PHP mysql数据库操作类,这个数据库操作类的特点是对所有保存到数据库的内容进入了过滤,可以很好的处理sql注入了,代码如下:
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……