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中的Class的几点个人看法
Oct 09 PHP
php提示Call-time pass-by-reference has been deprecated in的解决方法[已测]
May 06 PHP
php绘图中显示不出图片的原因及解决
Mar 05 PHP
PHP中读取文件的8种方法和代码实例
Aug 05 PHP
PHP GD库生成图像的几个函数总结
Nov 19 PHP
php+jquery+html实现点击不刷新加载更多的实例代码
Aug 12 PHP
浅谈PHP的反射API
Feb 26 PHP
PHP验证类的封装与使用方法详解
Jan 10 PHP
PHP goto语句用法实例
Aug 06 PHP
ThinkPHP 5.x远程命令执行漏洞复现
Sep 23 PHP
Laravel timestamps 设置为unix时间戳的方法
Oct 11 PHP
HTTP头隐藏PHP版本号实现过程解析
Dec 09 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入门学习知识点八 PHP中for循环基本应用之九九乘法口绝表
2011/07/14 PHP
源码分析 Laravel 重复执行同一个队列任务的原因
2017/12/25 PHP
PHP常见的序列化与反序列化操作实例分析
2019/10/28 PHP
laravel5.6 框架邮件队列database驱动简单demo示例
2020/01/26 PHP
JS option location 页面跳转实现代码
2008/12/27 Javascript
Jquery实现鼠标移上弹出提示框、移出消失思路及代码
2013/05/19 Javascript
js 控制页面跳转的5种方法
2013/09/09 Javascript
Jquery倒计时源码分享
2014/05/16 Javascript
js事件监听器用法实例详解
2015/06/01 Javascript
基于RequireJS和JQuery的模块化编程日常问题解析
2016/04/14 Javascript
Bootstrap的modal拖动效果
2016/12/25 Javascript
基于angular实现三级联动的生日插件
2017/05/12 Javascript
使用AngularJS编写多选按钮选中时触发指定方法的指令代码详解
2017/07/24 Javascript
jQuery实现文件编码成base64并通过AJAX上传的方法
2018/04/12 jQuery
vue使用代理解决请求跨域问题详解
2019/07/24 Javascript
微信公众号服务器验证Token步骤图解
2019/12/30 Javascript
vue 使用async写数字动态加载效果案例
2020/07/18 Javascript
[03:24]2014DOTA2国际邀请赛 神秘商店生意火爆
2014/07/18 DOTA
介绍Python中内置的itertools模块
2015/04/29 Python
Python中使用不同编码读写txt文件详解
2015/05/28 Python
Django 导出 Excel 代码的实例详解
2017/08/11 Python
Python实现修改文件内容的方法分析
2018/03/25 Python
Pycharm 操作Django Model的简单运用方法
2018/05/23 Python
解决pandas使用read_csv()读取文件遇到的问题
2018/06/15 Python
Python3操作读写CSV文件使用包过程解析
2020/04/10 Python
C语言笔试集
2012/07/24 面试题
火山动力Java笔试题
2014/06/26 面试题
工地安全检查制度
2014/02/04 职场文书
努力学习演讲稿
2014/05/10 职场文书
放飞梦想演讲稿200字
2014/08/26 职场文书
个人对照检查剖析材料
2014/10/13 职场文书
男生贾里读书笔记
2015/06/30 职场文书
2015大学迎新标语
2015/07/16 职场文书
2016年暑假学生家长评语
2015/12/01 职场文书
Java处理延时任务的常用几种解决方案
2022/06/01 Java/Android
使用CSS自定义属性实现骨架屏效果
2022/06/21 HTML / CSS