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 相关文章推荐
PHP 中文乱码解决办法总结分析
Jul 30 PHP
PHP调用Twitter的RSS的实现代码
Mar 10 PHP
PHP 冒泡排序算法的实现代码
Aug 08 PHP
PHP5权威编程阅读学习笔记 附电子书下载
Jul 05 PHP
php生成静态文件的多种方法分享
Jul 17 PHP
PHP实现下载功能的代码
Sep 29 PHP
完美解决thinkphp验证码出错无法显示的方法
Dec 09 PHP
php实现随机显示图片方法汇总
May 21 PHP
PHP实现无限级分类(不使用递归)
Oct 22 PHP
Symfony2实现从数据库获取数据的方法小结
Mar 18 PHP
thinkphp3.2.3版本的数据库增删改查实现代码
Sep 22 PHP
php表单文件iframe异步上传实例讲解
Jul 26 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版(4)
2006/10/09 PHP
《APMServ 5.1.2》使用图解
2006/10/23 PHP
php Smarty初体验二 获取配置信息
2011/08/08 PHP
一个PHP并发访问实例代码
2012/09/06 PHP
destoon各类调用汇总
2014/06/20 PHP
PHP中使用循环实现的金字塔图形
2014/11/08 PHP
PHP实现伪静态方法汇总
2016/01/13 PHP
初识PHP中的Swoole
2016/04/05 PHP
php+redis在实际项目中HTTP 500: Internal Server Error故障排除
2017/02/05 PHP
jQuery 使用手册(一)
2009/09/23 Javascript
基于jquery &amp; json的省市区联动代码
2012/06/26 Javascript
用js的for循环获取radio选中的值
2013/10/21 Javascript
文档对象模型DOM通俗讲解
2013/11/01 Javascript
javascript实现数字+字母验证码的简单实例
2014/02/10 Javascript
js返回上一页并刷新的多种实现方法
2014/02/26 Javascript
jquery实现弹出层遮罩效果的简单实例
2014/03/03 Javascript
javascript学习笔记(二)数组和对象部分
2014/09/30 Javascript
js实现input密码框提示信息的方法(附html5实现方法)
2016/01/14 Javascript
Vue.js教程之计算属性
2016/11/11 Javascript
详解vue-meta如何让你更优雅的管理头部标签
2018/01/18 Javascript
vue中实现图片和文件上传的示例代码
2018/03/16 Javascript
vue基于element的区间选择组件
2018/09/07 Javascript
小程序scroll-view安卓机隐藏横向滚动条的实现详解
2019/05/16 Javascript
关于NodeJS中的循环引用详解
2019/07/23 NodeJs
基于jQuery实现挂号平台首页源码
2020/01/06 jQuery
Python创建xml文件示例
2017/03/22 Python
Python字典数据对象拆分的简单实现方法
2017/12/05 Python
利用python GDAL库读写geotiff格式的遥感影像方法
2018/11/29 Python
屏蔽Django admin界面添加按钮的操作
2020/03/11 Python
基于Python爬取fofa网页端数据过程解析
2020/07/13 Python
诉前财产保全担保书
2014/05/20 职场文书
房屋维修协议书范本
2014/09/25 职场文书
学校师德师风整改措施
2014/10/27 职场文书
2015年第31个教师节致辞
2015/07/31 职场文书
高质量“欢迎词”
2019/04/03 职场文书
关于windows server 2012 DC 环境 重启后蓝屏代码:0xc00002e2的问题
2022/05/25 Servers