php写的带缓存数据功能的mysqli类

前端之家收集整理的这篇文章主要介绍了php写的带缓存数据功能的mysqli类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="54458" class="copybut" id="copybut54458" onclick="doCopy('code54458')"> 代码如下:

<div class="codebody" id="code54458">
<?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,$dbname);
if(MysqLi_connect_errno()) {
$this->MysqLi = false;
echo '

'.MysqLi_connect_error().'

';
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 "

".$this->MysqLi->error."

";
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,'getRow');
}
public function getAll($sql,$limit = null,$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,$fetch_mode = MysqLI_ASSOC) {
return $this->getAll($sql);
}
public function cache_all($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(),'
';
?>

原文链接:https://www.f2er.com/php/27012.html

猜你在找的PHP相关文章