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


Posted in PHP onSeptember 06, 2012
<?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 相关文章推荐
使用网络地址转换实现多服务器负载均衡
Oct 09 PHP
php intval的测试代码发现问题
Jul 27 PHP
如何利用PHP执行.SQL文件
Jul 05 PHP
PHP与Java进行通信的实现方法
Oct 21 PHP
php判断正常访问和外部访问的示例
Feb 10 PHP
解读PHP的Yii框架中请求与响应的处理流程
Mar 17 PHP
PHP递归实现汉诺塔问题的方法示例
Nov 25 PHP
php引用和拷贝的区别知识点总结
Sep 23 PHP
Windows服务器中PHP如何安装redis扩展
Sep 27 PHP
PHP实现单条sql执行多个数据的insert语句方法
Oct 11 PHP
php设计模式之策略模式实例分析【星际争霸游戏案例】
Mar 26 PHP
Swoole源码中如何查询Websocket的连接问题详解
Aug 30 PHP
一个PHP并发访问实例代码
Sep 06 #PHP
PHP连接MongoDB示例代码
Sep 06 #PHP
谨慎使用PHP的引用原因分析
Sep 06 #PHP
很让人受教的 提高php代码质量36计
Sep 05 #PHP
php控制linux服务器常用功能 关机 重启 开新站点等
Sep 05 #PHP
三个类概括PHP的五种设计模式
Sep 05 #PHP
用来解析.htpasswd文件的PHP类
Sep 05 #PHP
You might like
javascript 传统事件模型构造的事件监听器实现代码
2010/05/31 Javascript
一起来写段JS drag拖动代码
2010/12/09 Javascript
提示$ is not defined错误分析及解决
2013/04/09 Javascript
Javascript实现滑块滑动改变值的实现代码
2013/04/12 Javascript
javascript解决IE6下hover问题的方法
2015/07/28 Javascript
JS遍历页面所有对象属性及实现方法
2016/08/01 Javascript
JS转换HTML转义符的方法
2016/08/24 Javascript
详解angular element()方法使用
2017/04/08 Javascript
JavaScript实现多叉树的递归遍历和非递归遍历算法操作示例
2018/02/08 Javascript
微信小程序如何调用json数据接口并解析
2019/06/29 Javascript
js中script的上下放置区别,Dom的增删改创建操作实例分析
2019/12/16 Javascript
vue自定义标签和单页面多路由的实现代码
2020/05/03 Javascript
vue在响应头response中获取自定义headers操作
2020/07/24 Javascript
JavaScript 事件代理需要注意的地方
2020/09/08 Javascript
[04:38]完美世界携手游戏风云打造 卡尔工作室饰品系统篇
2013/04/25 DOTA
[01:13]这,就是刀塔
2014/07/16 DOTA
python 实现归并排序算法
2012/06/05 Python
Python单链表简单实现代码
2016/04/27 Python
Python设计实现的计算器功能完整实例
2017/08/18 Python
详解使用 pyenv 管理多个版本 python 环境
2017/10/19 Python
解决python3中解压zip文件是文件名乱码的问题
2018/03/22 Python
PyQt弹出式对话框的常用方法及标准按钮类型
2019/02/27 Python
python打开使用的方法
2019/09/30 Python
应届本科生推荐信范文
2013/12/25 职场文书
应届大学生简历中的自我评价
2014/01/15 职场文书
经贸韩语专业大学生职业规划
2014/02/14 职场文书
《青蛙看海》教学反思
2014/04/23 职场文书
干部作风整顿自我剖析材料和整改措施
2014/09/18 职场文书
领导班子四风对照检查材料思想汇报
2014/09/26 职场文书
机电专业毕业生自我鉴定2014
2014/10/04 职场文书
党员自我剖析材料(群众路线)
2014/10/06 职场文书
学习党的群众路线教育实践活动剖析材料
2014/10/13 职场文书
高一军训口号
2015/12/25 职场文书
Python获取百度热搜的完整代码
2021/04/07 Python
Tomcat用户管理的优化配置详解
2022/03/31 Servers
德劲DE1102数字调谐收音机机评
2022/04/07 无线电