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 相关文章推荐
WINDOWS 2000下使用ISAPI方式安装PHP
Sep 05 PHP
php ftp文件上传函数(基础版)
Jun 03 PHP
PHP学习之正则表达式
Apr 17 PHP
php遍历数组的方法分享
Mar 22 PHP
PHP $_FILES中error返回值详解
Jan 30 PHP
分享下页面关键字抓取components.arrow.com站点代码
Jan 30 PHP
php中$_GET与$_POST过滤sql注入的方法
Nov 03 PHP
PHP输出九九乘法表代码实例
Mar 27 PHP
WordPress开发中用于获取近期文章的PHP函数使用解析
Jan 05 PHP
php实现微信扫码自动登陆与注册功能
Sep 22 PHP
php文件包含目录配置open_basedir的使用与性能详解
Apr 03 PHP
PHP观察者模式示例【Laravel框架中有用到】
Jun 15 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缓存类代码(附详细说明)
2011/06/09 PHP
javascript Ext JS 状态默认存储时间
2009/02/15 Javascript
js下关于onmouseout、事件冒泡的问题经验小结
2010/12/09 Javascript
同一页面多个商品倒计时JS 基于面向对象的javascript
2012/02/16 Javascript
jQuery实现类似淘宝购物车全选状态示例
2013/06/26 Javascript
面向切面编程(AOP)的理解
2015/05/01 Javascript
javascript创建动态表单的方法
2015/07/25 Javascript
基于javascript编写简单日历
2016/05/02 Javascript
AngularJS变量及过滤器Filter用法分析
2016/11/22 Javascript
Bootstrap CSS组件之导航(nav)
2016/12/17 Javascript
php输出全部gb2312编码内的汉字方法
2017/03/04 Javascript
详解vue+css3做交互特效的方法
2017/11/20 Javascript
AngularJS双向数据绑定原理之$watch、$apply和$digest的应用
2018/01/30 Javascript
加快Vue项目的开发速度的方法
2018/12/12 Javascript
vue+iview/elementUi实现城市多选
2019/03/28 Javascript
JS回调函数 callback的理解与使用案例分析
2019/09/09 Javascript
微信小程序学习总结(五)常见问题实例小结
2020/06/04 Javascript
[59:00]DOTA2-DPC中国联赛 正赛 Ehome vs PSG.LGD BO3 第一场 3月7日
2021/03/11 DOTA
Python中tell()方法的使用详解
2015/05/24 Python
python机器学习理论与实战(二)决策树
2018/01/19 Python
Pandas之drop_duplicates:去除重复项方法
2018/04/18 Python
Python实现根据日期获取当天凌晨时间戳的方法示例
2019/04/09 Python
如何使用Flask-Migrate拓展数据库表结构
2019/07/24 Python
python 实现将小图片放到另一个较大的白色或黑色背景图片中
2019/12/12 Python
Selenium基于PIL实现拼接滚动截图
2020/04/10 Python
绿色城市实施方案
2014/03/19 职场文书
银行委托书范本
2014/04/04 职场文书
促销活动总结怎么写
2014/06/25 职场文书
走群众路线剖析材料
2014/10/09 职场文书
企业务虚会发言材料
2014/10/20 职场文书
保密工作整改报告
2014/11/06 职场文书
傲慢与偏见读书笔记
2015/06/29 职场文书
Python 实现定积分与二重定积分的操作
2021/05/26 Python
python+opencv实现视频抽帧示例代码
2021/06/11 Python
Spring Security使用单点登录的权限功能
2022/04/03 Java/Android
React四级菜单的实现
2022/04/08 Javascript