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

 更新时间:2012年09月06日 21:30:41   作者:  
本文分享一个带缓存数据功能的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实用代码片段

    必须收藏的php实用代码片段

    这篇文章主要为大家又分享了必须收藏的23个php实用代码片段,帮助大家更好地学习php程序设计,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • 使用array mutisort 实现按某字段对数据排序

    使用array mutisort 实现按某字段对数据排序

    本篇文章是对使用array mutisort 实现按某字段对数据排序的方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php实现子字符串位置相互对调互换的方法

    php实现子字符串位置相互对调互换的方法

    这篇文章主要介绍了php实现子字符串位置相互对调互换的方法,可实现简单字符串中两个子字符串互换的功能,涉及php字符串运算与插入、替换等操作的相关技巧,需要的朋友可以参考下
    2016-06-06
  • 纯php生成随机密码

    纯php生成随机密码

    这篇文章主要介绍了纯php生成12位随机密码,生成密码安全可靠,感兴趣的小伙伴们可以参考一下
    2015-10-10
  • PHP怎么实现网站保存快捷方式方便用户随时浏览

    PHP怎么实现网站保存快捷方式方便用户随时浏览

    网站保存快捷方式以后在浏览起来就比较方便了,实现的方法有很多,下面为大家详细介绍下使用PHP实现网站快捷方式的保存,有此需求的朋友可以参考下,希望对大家有所帮助
    2013-08-08
  • PHP Cookie学习笔记

    PHP Cookie学习笔记

    这篇文章主要为大家分享了PHP Cookie学习笔记,告诉大家什么是Cookie,Cookie的功能有哪些? 如何创建、读取、删除Cookie,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • CentOS6.5 编译安装lnmp环境

    CentOS6.5 编译安装lnmp环境

    这篇文章主要介绍了CentOS6.5 编译安装lnmp环境的相关资料及方法,需要的朋友可以参考下
    2014-12-12
  • php让图片可以下载的代码

    php让图片可以下载的代码

    让图片也能像附件一样的下载,不多说了。请看下面的程序!
    2008-09-09
  • PHP树的深度编历生成迷宫及A*自动寻路算法实例分析

    PHP树的深度编历生成迷宫及A*自动寻路算法实例分析

    这篇文章主要介绍了PHP树的深度编历生成迷宫及A*自动寻路算法,实例分析了php实现A*寻路算法的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • php连接Access数据库错误及解决方法

    php连接Access数据库错误及解决方法

    前二天把一个asp+access的网站改成php+access的,在连连数据库时可真让我狠狠的郁闷了一把,通过百度了大量的相关文章终于解决了
    2013-06-06

最新评论