首页 > 语言 > PHP > 正文

php缓存数据功能的mysqli类

2024-09-04 11:44:13
字体:
来源:转载
供稿:网友
  1. <?php    
  2.     /**   
  3.     * Mysqli类   
  4.     *   
  5.     * @author 废墟   
  6.     * @version v1.0 2009-08-18  
  7.     */    
  8.     class db_mysqli {    
  9.         protected $mysqli;    
  10.         protected $sql;    
  11.         protected $rs;    
  12.         protected $query_num    = 0;    
  13.         protected $fetch_mode    = MYSQLI_ASSOC;    
  14.         protected $cache_dir    = './cache/';    
  15.         protected $cache_time    = 1800;    
  16.   
  17.         public function  __construct($dbhost$dbuser$dbpass$dbname) {    
  18.             $this->mysqli    = new mysqli($dbhost$dbuser$dbpass$dbname);    
  19.             if(mysqli_connect_errno()) {    
  20.                 $this->mysqli    = false;    
  21.                 echo '<h2>'.mysqli_connect_error().'</h2>';    
  22.                 die();    
  23.             } else {    
  24.                 $this->mysqli->set_charset("utf8");    
  25.             }    
  26.         }    
  27.   
  28.         public function  __destruct() {    
  29.             $this->free();    
  30.             $this->close();    
  31.         }    
  32.   
  33.         protected function free() {    
  34.             @$this->rs->free();    
  35.         }    
  36.   
  37.         protected function close() {    
  38.             $this->mysqli->close();    
  39.         }    
  40.   
  41.         protected function fetch() {    
  42.             return $this->rs->fetch_array($this->fetch_mode);    
  43.         }    
  44.   
  45.         protected function getQuerySql($sql$limit = null) {    
  46.             if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is"$limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is"$sql)) {    
  47.                 $sql .= " LIMIT " . $limit;    
  48.             }    
  49.             return $sql;    
  50.         }    
  51.   
  52.         protected function get_cache($sql,$method) {    
  53.             include_once './cache.php';//若框架中使用__autoload(),这里可以不用加载文件    
  54.             $cache    = new cache($this->cache_dir,$this->cache_time);    
  55.             $cache_file    = md5($sql.$method);    
  56.             $res    = $cache->get_cache($cache_file);    
  57.             if(!$res) {    
  58.                 $res    = $this->$method($sql);    
  59.                 $cache->set_cache($cache_file$res);    
  60.             }    
  61.             return $res;    
  62.         }    
  63.   
  64.         public function query_num() {    
  65.             return $this->query_num;    
  66.         }    
  67.   
  68.         public function set_cache_dir($cache_dir) {    
  69.             $this->cache_dir    = $cache_dir;    
  70.         }    
  71.   
  72.         public function set_cache_time($cache_time) {    
  73.             $this->cache_time    = $cache_time;    
  74.         }    
  75.   
  76.         public function query($sql$limit = null) {    
  77.             $sql    = $this->getQuerySql($sql$limit);    
  78.             $this->sql    = $sql;    
  79.             $this->rs    = $this->mysqli->query($sql);    
  80.             if (!$this->rs) {    
  81.                 echo "<h2>".$this->mysqli->error."</h2>";    
  82.                 die();    
  83.             } else {    
  84.             $this->query_num++;    
  85.                 return $this->rs;    
  86.             }    
  87.         }    
  88.   
  89.         public function getOne($sql) {    
  90.             $this->query($sql, 1);    
  91.             $this->fetch_mode    = MYSQLI_NUM;    
  92.             $row = $this->fetch();    
  93.             $this->free();    
  94.             return $row[0];    
  95.         }    
  96.   
  97.         public function get_one($sql) {    
  98.             return $this->getOne($sql);    
  99.         }    
  100.   
  101.         public function cache_one($sql) {    
  102.             $sql    = $this->getQuerySql($sql, 1);    
  103.             return $this->get_cache($sql'getOne');    
  104.         }    
  105.   
  106.         public function getRow($sql$fetch_mode = MYSQLI_ASSOC) {    
  107.             $this->query($sql, 1);    
  108.             $this->fetch_mode    = $fetch_mode;    
  109.             $row = $this->fetch();    
  110.             $this->free();    
  111.             return $row;    
  112.         }    
  113.   
  114.         public function get_row($sql$fetch_mode = MYSQLI_ASSOC) {    
  115.             return $this->getRow($sql);   
  116.         }    
  117.   
  118.         public function cache_row($sql) {    
  119.             $sql    = $this->getQuerySql($sql, 1);    
  120.             return $this->get_cache($sql'getRow');    
  121.         }    
  122.   
  123.         public function getAll($sql$limit = null, $fetch_mode = MYSQLI_ASSOC) {    
  124.             $this->query($sql$limit);    
  125.             $all_rows = array();    
  126.             $this->fetch_mode    = $fetch_mode;    
  127.             while($rows = $this->fetch()) {    
  128.                 $all_rows[] = $rows;    
  129.             }    
  130.             $this->free();    
  131.             return $all_rows;    
  132.         }    
  133.         public function get_all($sql$limit = null, $fetch_mode = MYSQLI_ASSOC) {    
  134.             return $this->getAll($sql);    
  135.         }    
  136.   
  137.         public function cache_all($sql$limit = null) {    
  138.             $sql    = $this->getQuerySql($sql$limit);    
  139.             return $this->get_cache($sql'getAll');    
  140.         }    
  141.   
  142.         public function insert_id() {    
  143.             return $this->mysqli->insert_id();    
  144.         }    
  145.   
  146.         public function escape($str) {    
  147.             if(is_array($str)) {    
  148.                 foreach($str as $key=>$val) {    
  149.                     $str[$key] = $this->escape($val);    
  150.                 }    
  151.             } else {    
  152.                 $str = addslashes(trim($str));    
  153.             }   //开源代码Vevb.com 
  154.             return $str;    
  155.         }    
  156.     }    
  157.     //用法    
  158.     $db    = new db_mysqli('localhost''root', 111222, 'dict');    
  159.     $db->set_cache_time(10);    
  160.     $db->set_cache_dir('./cache/sql/');    
  161.     $sql = "select * from words order by word_id limit 10,10";    
  162.     $res1 = $db->get_all($sql);    
  163.     $res2 = $db->cache_all($sql);    
  164.   
  165.     echo $db->query_num(),'<br>';    
  166.     ?>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表