php写的带缓存数据功能的mysqli类

 更新时间:2012年09月06日 21:30:41   作者:  
本文分享一个带缓存数据功能的mysqli类,非常好用
复制代码 代码如下:

<?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判断两个给定日期是否在同一周的方法

    这篇文章主要介绍了PHP判断两个给定日期是否在同一周的方法,涉及PHP针对日期时间的转换、运算及判断等相关操作技巧,需要的朋友可以参考下
    2017-08-08
  • PHP目录操作实例总结

    PHP目录操作实例总结

    这篇文章主要介绍了PHP目录操作,结合实例形式总结分析了php针对目录的读取、遍历、关闭等常见操作的相关函数与使用技巧,需要的朋友可以参考下
    2016-09-09
  • PHP SPL使用方法和他的威力

    PHP SPL使用方法和他的威力

    什么是SPL,如何使用,他有什么作用,下面我我们就讲讲PHP SPL的用法
    2013-11-11
  • PHP中strtotime函数使用方法详解

    PHP中strtotime函数使用方法详解

    在PHP中有个叫做strtotime的函数。strtotime 实现功能:获取某个日期的时间戳,或获取某个时间的时间戳。strtotime 将任何英文文本的日期时间描述解析为Unix时间戳[将系统时间转化成unix时间戳]
    2011-11-11
  • PHP中for循环与foreach的区别

    PHP中for循环与foreach的区别

    本文主要介绍了php中for循环与foreach的区别,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • PHP操作MongoDB时的整数问题及对策说明

    PHP操作MongoDB时的整数问题及对策说明

    本文所说的整数问题,其实并不是MongoDB的问题,而是PHP驱动的问题
    2011-05-05
  • php实现的网页版剪刀石头布游戏示例

    php实现的网页版剪刀石头布游戏示例

    这篇文章主要介绍了php实现的网页版剪刀石头布游戏,涉及php数组遍历、比较及随机数组调用相关操作技巧,需要的朋友可以参考下
    2016-11-11
  • 简单实用的PHP防注入类实例

    简单实用的PHP防注入类实例

    这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下
    2014-12-12
  • php启动时候提示PHP startup的解决方法

    php启动时候提示PHP startup的解决方法

    配置好php环境后,每次开机都有警告提示说 PHP startup,解决这个问题很简单只需要在php.ini 文件中修改 extension_dir配置就行
    2013-05-05
  • PHP读取目录树的实现方法分析

    PHP读取目录树的实现方法分析

    这篇文章主要介绍了PHP读取目录树的实现方法,结合实例形式分析了php针对文件目录结构的遍历、读取操作实现技巧,需要的朋友可以参考下
    2019-03-03

最新评论