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写出自己的BLOG系统 2
Apr 12 PHP
PHP chmod 函数与批量修改文件目录权限
May 10 PHP
Codeigniter实现发送带附件的邮件
Mar 19 PHP
PHP添加图片水印、压缩、剪切的封装类
Aug 17 PHP
php邮件发送的两种方式
Apr 28 PHP
php中使用websocket详解
Sep 23 PHP
thinkphp分页集成实例
Jul 24 PHP
thinkphp中的多表关联查询的实例详解
Oct 12 PHP
PHP 断点续传实例详解
Nov 11 PHP
php实现将数据做成json的格式给前端使用
Aug 21 PHP
解决laravel groupBy 对查询结果进行分组出现的问题
Oct 09 PHP
php设计模式之正面模式实例分析【星际争霸游戏案例】
Mar 24 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/05/10 PHP
制作个性化的WordPress登陆界面的实例教程
2016/05/21 PHP
php 魔术常量详解及实例代码
2016/12/04 PHP
php四种定界符详解
2017/02/16 PHP
Yii2 队列 shmilyzxt/yii2-queue 简单概述
2017/08/02 PHP
XML+XSL 与 HTML 两种方案的结合
2007/04/22 Javascript
Javascript 函数对象的多重身份
2009/06/28 Javascript
文本框中,回车键触发事件的js代码[多浏览器兼容]
2010/06/07 Javascript
Javascript 判断是否存在函数的方法
2013/01/03 Javascript
js数组操作常用方法
2014/05/08 Javascript
基于javascript实现判断移动终端浏览器版本信息
2014/12/09 Javascript
javascript格式化日期时间方法汇总
2015/06/19 Javascript
JS+CSS实现带小三角指引的滑动门效果
2015/09/22 Javascript
js判断当前页面用什么浏览器打开的方法
2016/01/06 Javascript
EasyUI中在表单提交之前进行验证
2016/07/19 Javascript
Bootstrap整体框架之CSS12栅格系统
2016/12/15 Javascript
RequireJS 依赖关系的实例(推荐)
2017/01/21 Javascript
jquery实现tab键进行选择后enter键触发click行为
2017/03/29 jQuery
JavaScript原型继承_动力节点Java学院整理
2017/06/30 Javascript
jQuery实现的文字逐行向上间歇滚动效果示例
2017/09/06 jQuery
浅谈Vue Element中Select下拉框选取值的问题
2018/03/01 Javascript
VUE2.0 ElementUI2.0表格el-table自适应高度的实现方法
2018/11/28 Javascript
[49:31]TFT vs Mski Supermajor小组赛C组 BO3 第一场 6.3
2018/06/04 DOTA
在Django的URLconf中使用多个视图前缀的方法
2015/07/18 Python
常见python正则用法的简单实例
2016/06/21 Python
Python实现栈和队列的简单操作方法示例
2019/11/29 Python
python GUI库图形界面开发之PyQt5布局控件QGridLayout详细使用方法与实例
2020/03/06 Python
python正则表达式re.match()匹配多个字符方法的实现
2021/01/27 Python
三维科技面试题
2013/07/27 面试题
特色蛋糕店创业计划书
2014/01/28 职场文书
幼儿园课题方案
2014/06/09 职场文书
我的梦想演讲稿500字
2014/08/21 职场文书
2014年祖国生日寄语
2014/09/19 职场文书
银行授权委托书范本
2014/10/04 职场文书
放假通知
2015/04/14 职场文书
使用vue-element-admin框架从后端动态获取菜单功能的实现
2021/04/29 Vue.js