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 中的类
Oct 09 PHP
dedecms系统的广告设置代码 基础版本
Apr 09 PHP
linux下删除7天前日志的代码(php+shell)
Jan 02 PHP
解析PHP中DIRECTORY_SEPARATOR,PATH_SEPARATOR两个常量的作用
Jun 21 PHP
php实现根据IP地址获取其所在省市的方法
Apr 30 PHP
PHP中empty和isset对于参数结构的判断及empty()和isset()的区别
Nov 15 PHP
PHP实现简单ajax Loading加载功能示例
Dec 28 PHP
简单谈谈 php 文件锁
Feb 19 PHP
thinkPHP框架实现图像裁剪、缩放、加水印的方法
Mar 14 PHP
浅谈php://filter的妙用
Mar 05 PHP
Laravel框架集合用法实例浅析
May 14 PHP
七种PHP开发环境搭建工具
Jun 28 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函数间的参数传递(值传递/引用传递)
2013/09/23 PHP
Linux下PHP安装mcrypt扩展模块笔记
2014/09/10 PHP
在Windows系统下使用PHP生成Word文档的教程
2015/07/03 PHP
正则表达式判断是否存在中文和全角字符和判断包含中文字符串长度
2008/09/27 Javascript
Javascript的并行运算实现代码
2010/11/19 Javascript
js 自动播放的实例代码
2013/11/19 Javascript
js日期范围初始化得到前一个月日期的方法
2015/05/05 Javascript
JavaScript实现对下拉列表值进行排序的方法
2015/07/15 Javascript
为何JS操作的href都是javascript:void(0);呢
2015/11/12 Javascript
jQuery实现简单的文件上传进度条效果
2020/03/26 Javascript
json格式的javascript对象用法分析
2016/07/04 Javascript
AngularJS入门教程之模块化操作用法示例
2016/11/02 Javascript
Vue项目中quill-editor带样式编辑器的使用方法
2017/08/08 Javascript
javascript+jQuery实现360开机时间显示效果
2017/11/03 jQuery
常用的 JS 排序算法 整理版
2018/04/05 Javascript
通过实例了解js函数中参数的传递
2019/06/15 Javascript
Vue 路由间跳转和新开窗口的方式(query、params)
2019/12/25 Javascript
vue v-model的用法解析
2020/10/19 Javascript
五句话帮你轻松搞定js原型链
2020/12/09 Javascript
JavaScript this关键字的深入详解
2021/01/14 Javascript
python中pip的安装与使用教程
2018/08/10 Python
Django框架首页和登录页分离操作示例
2019/05/28 Python
通过cmd进入python的实例操作
2019/06/26 Python
python打包exe开机自动启动的实例(windows)
2019/06/28 Python
django表单的Widgets使用详解
2019/07/22 Python
Pytorch反向求导更新网络参数的方法
2019/08/17 Python
python绘制彩虹图
2019/12/16 Python
Python sklearn库实现PCA教程(以鸢尾花分类为例)
2020/02/24 Python
用python 绘制茎叶图和复合饼图
2021/02/26 Python
定制别致的瑜伽垫:Sugarmat
2019/06/21 全球购物
马智宇结婚主持词
2014/04/01 职场文书
社区护士演讲稿
2014/08/27 职场文书
2014年销售人员工作总结
2014/11/27 职场文书
小学老师对学生的评语
2014/12/29 职场文书
Nginx解决前端访问资源跨域问题的方法详解
2021/03/31 Servers
JavaScript中的LHS和RHS分析详情
2022/04/06 Javascript