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 相关文章推荐
回答PHPCHINA上的几个问题:URL映射
Feb 14 PHP
那些年一起学习的PHP(三)
Mar 22 PHP
php FLEA中二叉树数组的遍历输出
Sep 26 PHP
php curl的深入解析
Jun 02 PHP
php curl模拟post请求小实例
Nov 13 PHP
PHP图片等比例缩放生成缩略图函数分享
Jun 10 PHP
php自定义错误处理用法实例
Mar 20 PHP
php语言注释,单行注释和多行注释
Jan 21 PHP
php引用和拷贝的区别知识点总结
Sep 23 PHP
laravel excel 上传文件保存到本地服务器功能
Nov 14 PHP
PHP设计模式之适配器模式(Adapter)原理与用法详解
Dec 12 PHP
一文搞懂php的垃圾回收机制
Jun 18 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 include_path设置技巧分享
2011/07/03 PHP
PHP快速生成各种信息提示框的方法
2016/02/03 PHP
注释PHP和html混合代码的小技巧(分享)
2016/11/03 PHP
PHP判断文件是否被引入的方法get_included_files用法示例
2016/11/29 PHP
php抽象方法和抽象类实例分析
2016/12/07 PHP
php获取ip及网址的简单方法(必看)
2017/04/01 PHP
php简单随机字符串生成方法示例
2017/04/19 PHP
PHP框架Laravel中使用UUID实现数据分表操作示例
2018/05/30 PHP
jquery图片上下tab切换效果
2011/03/18 Javascript
超简单JS二级、多级联动的简单实例
2014/02/18 Javascript
jquery插件validation实现验证身份证号等
2015/06/04 Javascript
理解Javascript的动态语言特性
2015/06/17 Javascript
基于jQuery通过jQuery.form.js插件实现异步上传
2015/12/13 Javascript
很全面的JavaScript常用功能汇总集合
2016/01/22 Javascript
jquery点赞功能实现代码 点个赞吧!
2020/05/29 jQuery
使用 Node.js 对文本内容分词和关键词抽取
2017/05/27 Javascript
JS中offset和匀速动画详解
2018/02/06 Javascript
layui操作列按钮个数和文字颜色的判断实例
2019/09/11 Javascript
Javascript如何实现双指控制图片功能
2020/02/25 Javascript
vue使用axios实现excel文件下载的功能
2020/07/16 Javascript
Python中logging模块的用法实例
2014/09/29 Python
Python多进程分块读取超大文件的方法
2016/04/13 Python
基于pandas数据样本行列选取的方法
2018/04/20 Python
Python3之读取连接过的网络并定位的方法
2018/04/22 Python
python多线程之事件Event的使用详解
2018/04/27 Python
Python标准库使用OrderedDict类的实例讲解
2019/02/14 Python
python操作日志的封装方法(两种方法)
2019/05/23 Python
Python 限定函数参数的类型及默认值方式
2019/12/24 Python
css3实例教程 一款纯css3实现的发光屏幕旋转特效
2014/12/07 HTML / CSS
Expedia加拿大官方网站:加拿大最大的在线旅游提供商
2017/12/31 全球购物
耐克奥地利官网:Nike奥地利
2019/08/16 全球购物
中学教师请假制度
2014/02/03 职场文书
驾驶员安全责任书范本
2014/07/24 职场文书
python缺失值的解决方法总结
2021/06/09 Python
Python基于百度API识别并提取图片中文字
2021/06/27 Python
python人工智能human learn绘图可创建机器学习模型
2021/11/23 Python