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 相关文章推荐
第十节 抽象方法和抽象类 [10]
Oct 09 PHP
PHP操作文件方法问答
Mar 16 PHP
IStream与TStream之间的相互转换
Aug 01 PHP
Discuz 模板语句分析及知识技巧
Aug 21 PHP
PHP中获取变量的变量名的一段代码的bug分析
Jul 07 PHP
php数组函数序列之array_pop() - 删除数组中的最后一个元素
Nov 07 PHP
深入php函数file_get_contents超时处理的方法详解
Jun 03 PHP
分享下PHP register_globals 值为on与off的理解
Sep 26 PHP
php实现html标签闭合检测与修复方法
Jul 09 PHP
Zend Framework生成验证码并实现验证码验证功能(附demo源码下载)
Mar 22 PHP
PHP图形计数器程序显示网站用户浏览量
Jul 20 PHP
PHP 用session与gd库实现简单验证码生成与验证的类方法
Nov 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
漫荒推荐:画风超赞的国风漫画推荐 超长假期不无聊
2020/03/08 国漫
php用数组返回无限分类的列表数据的代码
2010/08/08 PHP
PHP获取表单textarea数据中的换行问题
2010/09/10 PHP
jquery动画3.创建一个带遮罩效果的图片走廊
2012/08/24 Javascript
浅谈JavaScript中的字符编码转换问题
2015/07/07 Javascript
在Javascript操作JSON对象,增加 删除 修改的简单实现
2016/06/02 Javascript
javascript cookie用法基础教程(概念,设置,读取及删除)
2016/09/20 Javascript
angularJS 指令封装回到顶部示例详解
2017/01/22 Javascript
实例讲解DataTables固定表格宽度(设置横向滚动条)
2017/07/11 Javascript
Angular4学习笔记之准备和环境搭建项目
2017/08/01 Javascript
通过一个简单的例子学会vuex与模块化
2017/11/22 Javascript
vue 使用Jade模板写html,stylus写css的方法
2018/02/23 Javascript
浅谈在react中如何实现扫码枪输入
2018/07/04 Javascript
基于Node.js的大文件分片上传示例
2019/06/19 Javascript
Vue toFixed保留两位小数的3种方式
2020/10/23 Javascript
[02:54]DOTA2英雄基础教程 暗影牧师戴泽
2013/12/05 DOTA
Python如何通过subprocess调用adb命令详解
2017/08/27 Python
Python 利用高德地图api实现经纬度与地址的批量转换
2019/08/14 Python
Python爬虫 urllib2的使用方法详解
2019/09/23 Python
Python爬取爱奇艺电影信息代码实例
2019/11/26 Python
TFRecord文件查看包含的所有Features代码
2020/02/17 Python
Jupyter notebook如何修改平台字体
2020/05/13 Python
python3处理word文档实例分析
2020/12/01 Python
python 基于PYMYSQL使用MYSQL数据库
2020/12/24 Python
使用CSS3编写灰阶滤镜来制作黑白照片效果的方法
2016/05/09 HTML / CSS
html5实现多图片预览上传及点击可拖拽控件
2018/03/15 HTML / CSS
eBay美国官网:eBay.com
2020/10/24 全球购物
Harrods英国:世界领先的奢侈品百货商店
2020/09/23 全球购物
中东奢侈品购物网站:Ounass
2020/09/02 全球购物
美容院考勤制度
2014/01/30 职场文书
倡议书格式模板
2014/05/13 职场文书
运动会宣传口号
2014/06/09 职场文书
小学生推普周国旗下讲话稿
2014/09/21 职场文书
2015年护士医德医风自我评价
2015/03/03 职场文书
2015学校六五普法工作总结
2015/04/22 职场文书
uniapp开发打包多端应用完整方法指南
2022/12/24 Javascript