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
一个没有MYSQL数据库支持的简易留言本的编写
Oct 09 PHP
PHP+JS无限级可伸缩菜单详解(简单易懂)
Jan 02 PHP
php 将bmp图片转为jpg等其他任意格式的图片
Jun 21 PHP
php数组函数序列之in_array() - 查找数组中是否存在指定值
Nov 07 PHP
php判断是否为json格式的方法
Mar 04 PHP
php cli换行示例
Apr 22 PHP
用PHP解决的一个栈的面试题
Jul 02 PHP
php中使用array_filter()函数过滤空数组的实现代码
Aug 19 PHP
PHP7.0版本备注
Jul 23 PHP
thinkPHP中钩子的使用方法实例分析
Nov 16 PHP
PHP使用SOAP调用API操作示例
Dec 25 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
php之XML转数组函数的详解
2013/06/07 PHP
joomla jce editor 解决上传中文名文件失败问题
2013/06/09 PHP
PHP中的reflection反射机制测试例子
2014/08/05 PHP
php快速查找数据库中恶意代码的方法
2015/04/01 PHP
php集成动态口令认证
2016/07/21 PHP
jquery 表单下所有元素的隐藏
2009/07/25 Javascript
用js调用迅雷下载代码的二种方法
2013/04/15 Javascript
js中的preventDefault与stopPropagation详解
2014/01/29 Javascript
jquery禁止输入数字以外的字符的示例(纯数字验证码)
2014/04/10 Javascript
JavaScript的RequireJS库入门指南
2015/07/01 Javascript
jquery Easyui快速开发总结
2015/08/20 Javascript
一个超简单的jQuery回调函数例子(分享)
2016/08/08 Javascript
JavaScript实现的CRC32函数示例
2016/11/23 Javascript
利用JS轻松实现获取表单数据
2016/12/06 Javascript
基于node打包可执行文件工具_Pkg使用心得分享
2018/01/24 Javascript
JS中的JSON对象的定义和取值实现代码
2018/05/09 Javascript
React Native中Mobx的使用方法详解
2018/12/04 Javascript
createObjectURL方法实现本地图片预览
2019/09/30 Javascript
Python使用wxPython实现计算器
2018/01/30 Python
python输入多行字符串的方法总结
2019/07/02 Python
在python 中split()使用多符号分割的例子
2019/07/15 Python
Python代码生成视频的缩略图的实例讲解
2019/12/22 Python
python设置环境变量的作用整理
2020/02/17 Python
Python爬虫新手入门之初学lxml库
2020/12/20 Python
html5默认气泡修改的代码详解
2020/03/13 HTML / CSS
日本一家专门经营各种箱包的大型网站:Traveler Store
2016/08/03 全球购物
美国最大的珠宝商之一:Littman Jewelers
2016/11/13 全球购物
Asics日本官网:鬼冢八喜郎创立的跑鞋运动品牌
2017/10/18 全球购物
中文师范生自荐信
2014/01/30 职场文书
座谈会主持词
2014/03/20 职场文书
师德师风自查总结
2014/10/14 职场文书
2015年学校关工委工作总结
2015/04/03 职场文书
投资申请报告
2015/05/19 职场文书
团队拓展训练心得体会
2016/01/12 职场文书
不同品牌、不同型号对讲机如何互相通联
2022/02/18 无线电
flex布局中使用flex-wrap实现换行的项目实践
2022/06/21 HTML / CSS