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 相关文章推荐
实用函数3
Nov 08 PHP
MYSQL 小技巧 -- LAST_INSERT_ID
Nov 24 PHP
Warning: session_destroy() : Trying to destroy uninitialized sessionq错误
Jun 16 PHP
Smarty中调用FCKeditor的方法
Oct 27 PHP
Ubuntu下安装PHP的mongodb扩展操作命令
Jul 04 PHP
php删除数组中重复元素的方法
Dec 22 PHP
PHP中抽象类、接口的区别与选择分析
Mar 29 PHP
PHP 等比例缩放图片详解及实例代码
Sep 18 PHP
PHP实现多关键字加亮功能
Oct 21 PHP
PHP基于接口技术实现简单的多态应用完整实例
Apr 26 PHP
PHP jQuery+Ajax结合写批量删除功能
May 19 PHP
Yii2语言国际化自动配置详解
Aug 22 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常用代码大全(新手入门必备)
2010/06/29 PHP
php socket通信(tcp/udp)实例分析
2016/02/14 PHP
PHP在linux上执行外部命令的方法
2017/02/06 PHP
Laravel关联模型中过滤结果为空的结果集(has和with区别)
2018/10/18 PHP
javascript 面向对象编程 聊聊对象的事
2009/09/17 Javascript
在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
2011/06/02 Javascript
jquery实现简单的拖拽效果实例兼容所有主流浏览器(优化篇)
2013/06/28 Javascript
js控制table合并具体实现
2014/02/20 Javascript
jQuery DOM操作实例
2014/03/05 Javascript
给js文件传参数(详解)
2014/07/13 Javascript
setTimeout()递归调用不加引号出错的解决方法
2014/09/05 Javascript
jQuery仿淘宝网产品品牌隐藏与显示效果
2015/09/01 Javascript
easyui tree带checkbox实现单选的简单实例
2016/11/07 Javascript
js鼠标跟随运动效果
2017/03/11 Javascript
如何使用Bootstrap 按钮实例详解
2017/03/29 Javascript
微信小程序简单实现form表单获取输入数据功能示例
2017/11/30 Javascript
vue.js vue-router如何实现无效路由(404)的友好提示
2017/12/20 Javascript
webpack 如何同时输出压缩和未压缩的文件的实现步骤
2020/06/05 Javascript
TypeScript 运行时类型检查补充工具
2020/09/28 Javascript
多个Vue项目部署到服务器的步骤记录
2020/10/22 Javascript
python从入门到精通(DAY 3)
2015/12/20 Python
在Windows系统上搭建Nginx+Python+MySQL环境的教程
2015/12/25 Python
Python实现屏幕截图的代码及函数详解
2016/10/01 Python
解决python3 urllib中urlopen报错的问题
2017/03/25 Python
Django视图和URL配置详解
2018/01/31 Python
Python利用openpyxl库遍历Sheet的实例
2018/05/03 Python
使用Django启动命令行及执行脚本的方法
2018/05/29 Python
python文件操作seek()偏移量,读取指正到指定位置操作
2020/07/05 Python
详解如何修改python中字典的键和值
2020/09/29 Python
Python就将所有的英文单词首字母变成大写
2021/02/12 Python
南非领先的在线旅行社:Travelstart南非
2016/09/04 全球购物
社区学雷锋活动策划方案
2014/01/30 职场文书
2014学生会工作总结报告
2014/12/02 职场文书
综合实践活动报告
2015/02/05 职场文书
护士个人总结范文
2015/02/13 职场文书
VUE使用draggable实现组件拖拽
2022/04/06 Vue.js