php寫的帶緩存數(shù)據(jù)功能的mysqli類
更新時間:2012年09月06日 21:30:41 作者:
本文分享一個帶緩存數(shù)據(jù)功能的mysqli類,非常好用
復制代碼 代碼如下:
<?php
/**
* Mysqli類
*/
class db_mysqli {
protected $mysqli;
protected $sql;
protected $rs;
protected $query_num = 0;
protected $fetch_mode = MYSQLI_ASSOC;
protected $cache_dir = './cache/';
protected $cache_time = 1800;
public function __construct($dbhost, $dbuser, $dbpass, $dbname) {
$this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()) {
$this->mysqli = false;
echo '<h2>'.mysqli_connect_error().'</h2>';
die();
} else {
$this->mysqli->set_charset("utf8");
}
}
public function __destruct() {
$this->free();
$this->close();
}
protected function free() {
@$this->rs->free();
}
protected function close() {
$this->mysqli->close();
}
protected function fetch() {
return $this->rs->fetch_array($this->fetch_mode);
}
protected function getQuerySql($sql, $limit = null) {
if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is", $limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is", $sql)) {
$sql .= " LIMIT " . $limit;
}
return $sql;
}
protected function get_cache($sql,$method) {
include_once './cache.php';//若框架中使用__autoload(),這里可以不用加載文件
$cache = new cache($this->cache_dir,$this->cache_time);
$cache_file = md5($sql.$method);
$res = $cache->get_cache($cache_file);
if(!$res) {
$res = $this->$method($sql);
$cache->set_cache($cache_file, $res);
}
return $res;
}
public function query_num() {
return $this->query_num;
}
public function set_cache_dir($cache_dir) {
$this->cache_dir = $cache_dir;
}
public function set_cache_time($cache_time) {
$this->cache_time = $cache_time;
}
public function query($sql, $limit = null) {
$sql = $this->getQuerySql($sql, $limit);
$this->sql = $sql;
$this->rs = $this->mysqli->query($sql);
if (!$this->rs) {
echo "<h2>".$this->mysqli->error."</h2>";
die();
} else {
$this->query_num++;
return $this->rs;
}
}
public function getOne($sql) {
$this->query($sql, 1);
$this->fetch_mode = MYSQLI_NUM;
$row = $this->fetch();
$this->free();
return $row[0];
}
public function get_one($sql) {
return $this->getOne($sql);
}
public function cache_one($sql) {
$sql = $this->getQuerySql($sql, 1);
return $this->get_cache($sql, 'getOne');
}
public function getRow($sql, $fetch_mode = MYSQLI_ASSOC) {
$this->query($sql, 1);
$this->fetch_mode = $fetch_mode;
$row = $this->fetch();
$this->free();
return $row;
}
public function get_row($sql, $fetch_mode = MYSQLI_ASSOC) {
return $this->getRow($sql);
}
public function cache_row($sql) {
$sql = $this->getQuerySql($sql, 1);
return $this->get_cache($sql, 'getRow');
}
public function getAll($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
$this->query($sql, $limit);
$all_rows = array();
$this->fetch_mode = $fetch_mode;
while($rows = $this->fetch()) {
$all_rows[] = $rows;
}
$this->free();
return $all_rows;
}
public function get_all($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
return $this->getAll($sql);
}
public function cache_all($sql, $limit = null) {
$sql = $this->getQuerySql($sql, $limit);
return $this->get_cache($sql, 'getAll');
}
public function insert_id() {
return $this->mysqli->insert_id();
}
public function escape($str) {
if(is_array($str)) {
foreach($str as $key=>$val) {
$str[$key] = $this->escape($val);
}
} else {
$str = addslashes(trim($str));
}
return $str;
}
}
//用法
$db = new db_mysqli('localhost', 'root', 111222, 'dict');
$db->set_cache_time(10);
$db->set_cache_dir('./cache/sql/');
$sql = "select * from words order by word_id limit 10,10";
$res1 = $db->get_all($sql);
$res2 = $db->cache_all($sql);
echo $db->query_num(),'<br>';
?>
您可能感興趣的文章:
- PHP的Yii框架中Model模型的學習教程
- PHP YII框架開發(fā)小技巧之模型(models)中rules自定義驗證規(guī)則
- php數(shù)據(jù)庫操作model類(使用__call方法)
- php實現(xiàn)的簡單數(shù)據(jù)庫操作Model類
- PHP實現(xiàn)基于mysqli的Model基類完整實例
- PHP數(shù)據(jù)庫操作之基于Mysqli的數(shù)據(jù)庫操作類庫
- php封裝的mysqli類完整實例
- PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫連接工具類【定義與用法】
- PHP封裝的mysqli數(shù)據(jù)庫操作類示例
- PHP封裝mysqli基于面向對象的mysql數(shù)據(jù)庫操作類與用法示例
- PHP模型Model類封裝數(shù)據(jù)庫操作示例
相關文章
PHP調用全國天氣預報數(shù)據(jù)接口查詢天氣示例
這篇文章主要介紹了PHP調用全國天氣預報數(shù)據(jù)接口查詢天氣,涉及第三方平臺的key申請、接口數(shù)據(jù)調用及curl相關操作技巧,需要的朋友可以參考下2019-02-02使用WordPress發(fā)送電子郵件的相關PHP函數(shù)用法解析
這篇文章主要介紹了使用WordPress發(fā)送電子郵件的相關PHP函數(shù)用法解析,文中還提到了常見的郵件無法發(fā)送的情況的解決,需要的朋友可以參考下2015-12-12