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开发工具之vs2005图解
Jan 12 PHP
php 运行效率总结(提示程序速度)
Nov 26 PHP
PHPWind 发帖回帖Api PHP版打包下载
Feb 08 PHP
openflashchart 2.0 简单案例php版
May 21 PHP
PHP学习笔记 IIS7下安装配置php环境
Oct 29 PHP
PHP根据传入参数合并多个JS和CSS文件的简单实现
Jun 13 PHP
PHP导入导出Excel代码
Jul 07 PHP
PHP实现163邮箱自动发送邮件
Mar 29 PHP
ThinkPHP3.2.1图片验证码实现方法
Aug 19 PHP
基于php中echo用逗号和用点号的区别详解
Jan 23 PHP
Ajax+PHP实现的删除数据功能示例
Feb 12 PHP
PHP远程连接oracle数据库操作实现方法图文详解
Apr 11 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生成txt文件标题及内容的方法
2014/01/16 PHP
基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码(带平滑移动的效果)
2011/05/24 Javascript
远离JS灾难css灾难之 js私有函数和css选择器作为容器
2011/12/11 Javascript
分享8款优秀的 jQuery 加载动画和进度条插件
2012/10/24 Javascript
jQuery实用基础超详细介绍
2013/04/11 Javascript
用IE重起计算机或者关机的示例代码
2014/03/10 Javascript
js实现可得到不同颜色值的颜色选择器实例
2015/02/28 Javascript
javascript中this指向详解
2016/04/23 Javascript
完美JQuery图片切换效果的简单实现
2016/07/21 Javascript
详解js中的apply与call的用法
2016/07/30 Javascript
javascript之IE版本检测超简单方法
2016/08/20 Javascript
微信小程序 欢迎界面开发的实例详解
2016/11/30 Javascript
Layui数据表格之获取表格中所有的数据方法
2018/08/20 Javascript
原生js检测页面加载完毕的实例
2018/09/11 Javascript
在vue中使用v-bind:class的选项卡方法
2018/09/27 Javascript
jQuery实现获取当前鼠标位置并输出功能示例
2019/01/05 jQuery
微信小程序 image组件遇到的问题
2019/05/28 Javascript
layui layer select 选择被遮挡的解决方法
2019/09/21 Javascript
python基础入门学习笔记(Python环境搭建)
2016/01/13 Python
python自制包并用pip免提交到pypi仅安装到本机【推荐】
2019/06/03 Python
python pillow模块使用方法详解
2019/08/30 Python
利用Python裁切tiff图像且读取tiff,shp文件的实例
2020/03/10 Python
Django Admin后台添加数据库视图过程解析
2020/04/01 Python
numpy矩阵数值太多不能全部显示的解决
2020/05/14 Python
获取python运行输出的数据并解析存为dataFrame实例
2020/07/07 Python
HTML5 实战PHP之Web页面表单设计
2011/10/09 HTML / CSS
美国酒店控股公司:Choice Hotels
2018/06/15 全球购物
英国标准协会商店:BSI Shop
2019/02/25 全球购物
初中数学教学反思
2014/01/16 职场文书
机票销售员态度不好检讨书
2014/09/27 职场文书
文明旅游倡议书
2015/04/28 职场文书
初中政教处工作总结
2015/08/12 职场文书
Spring中bean的生命周期之getSingleton方法
2021/06/30 Java/Android
Redis做数据持久化的解决方案及底层原理
2021/07/15 Redis
微软Win11有哪些隐藏功能? windows11多个功能汇总
2021/11/21 数码科技
TypeScript 内置高级类型编程示例
2022/09/23 Javascript