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中使用反射技术的架构插件使用说明
May 18 PHP
php读取mysql中文数据出现乱码的解决方法
Aug 16 PHP
php实现文件下载简单示例(代码实现文件下载)
Mar 10 PHP
PHP多进程编程实例
Oct 15 PHP
PHP实现清除wordpress里恶意代码
Oct 21 PHP
PHP读取大文件的几种方法介绍
Oct 27 PHP
简单解决微信文章图片防盗链问题
Dec 17 PHP
php记录搜索引擎爬行记录的实现代码
Mar 02 PHP
php-fpm服务启动脚本的方法
Apr 27 PHP
smarty模板的使用方法实例分析
Sep 18 PHP
Laravel框架Eloquent ORM修改数据操作示例
Dec 03 PHP
phpcmsv9.0任意文件上传漏洞解析
Oct 20 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
10个实用的PHP代码片段
2011/09/02 PHP
php第一次无法获取cookie问题处理
2014/12/15 PHP
laravel添加前台跳转成功页面示例
2019/10/22 PHP
jQuery学习笔记之jQuery选择器的使用
2010/12/22 Javascript
JavaScript和CSS通过expression实现Table居中显示
2013/06/28 Javascript
JavaScript中的prototype.bind()方法介绍
2014/04/04 Javascript
通过伪协议解决父页面与iframe页面通信的问题
2015/04/05 Javascript
js改变embed标签src值的方法
2015/04/10 Javascript
原生js制作简单的数字键盘
2015/04/24 Javascript
js实现搜索框关键字智能匹配代码
2020/03/26 Javascript
JS输出空格的简单实现方法
2016/09/08 Javascript
js仿新浪微博消息发布功能
2017/02/17 Javascript
vue项目常用组件和框架结构介绍
2017/12/24 Javascript
jQuery实现的点击标题文字切换字体效果示例【测试可用】
2018/04/26 jQuery
记React connect的几种写法(小结)
2018/09/18 Javascript
vue项目环境变量配置的实现方法
2018/10/12 Javascript
提升node.js中使用redis的性能遇到的问题及解决方法
2018/10/30 Javascript
Vue拖拽组件列表实现动态页面配置功能
2019/06/17 Javascript
基于ssm框架实现layui分页效果
2019/07/27 Javascript
解决layui的table插件无法多层级获取json数据的问题
2019/09/19 Javascript
详解python使用递归、尾递归、循环三种方式实现斐波那契数列
2018/01/16 Python
Python线程创建和终止实例代码
2018/01/20 Python
python selenium自动上传有赞单号的操作方法
2018/07/05 Python
python opencv实现切变换 不裁减图片
2018/07/26 Python
Spring实战之使用util:命名空间简化配置操作示例
2019/12/09 Python
Django model.py表单设置默认值允许为空的操作
2020/05/19 Python
python使用nibabel和sitk读取保存nii.gz文件实例
2020/07/01 Python
乌克兰第一的珠宝网上商店:Gold.ua
2019/11/29 全球购物
ParcelABC西班牙:包裹运送和快递服务
2019/12/24 全球购物
美国家居装饰购物网站:Amanda Lindroth
2020/03/25 全球购物
璀璨的珍珠、密钉和个性化珠宝:Lily & Roo
2021/01/21 全球购物
大学生村官座谈会发言材料
2014/05/25 职场文书
人代会标语
2014/06/30 职场文书
工程部文员岗位职责
2015/02/04 职场文书
2019学校运动会开幕词
2019/05/13 职场文书
Go 实现英尺和米的简单单位换算方式
2021/04/29 Golang