写的一个mysqli类,带cache,讨论下filecache和mysql本身的效率

写的一个mysqli类,带cache,讨论下filecache和mysql本身的效率
[code]
<?php
/**
* Mysqli类
*
* @author 废墟
* @version v1.0 2009-08-18
* @link
http://anerg.cn/
*/
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>';
?>

[/code]

以下是cache类
[code]
<?php
/**
* 文件缓存类
*
* @author 废墟
* @version v1.01 2009-08-18
* @link
http://anerg.cn/
*/
class cache {
    private $cache_time;
    private $cache_dir;

    public function __construct($cache_dir = './cache/', $cache_time = 3600) {
        $this->cache_time    = $cache_time;
        $this->cache_dir    = $cache_dir;
    }

    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 get_cache($cache_file) {
        $_CACHE = array();
        $filename = $this->cache_dir.'/'.$cache_file.".cache.php";

        if(!file_exists($filename) || time() - filemtime($filename) > $this->cache_time) {
            return false;
        } else {
            return unserialize(file_get_contents($filename));
        }
    }

    public function set_cache($cache_file,$data) {
        $filename = $this->cache_dir.'/'.$cache_file.".cache.php";
        if( $this->mkpath($this->cache_dir) ) {
            $out    = serialize($data);
            file_put_contents($filename, $out);
        }
    }

    public function del_cache($cache_file) {
        return unlink($this->cache_dir.'/'.$cache_file.".cache.php");
    }

    public function mkpath($dir) {
        return is_dir($dir) or ($this->mkpath(dirname($dir)) and (mkdir($dir, 0777) and chmod($dir,0777)));
    }
}
//
//$cache = new cache();
//$cache->set_cache_dir('./cache/ss/ff/mm/');
//$cache->set_cache_time(10);
//$cache_file    = '321ewqe3412132';
//$data    = array("1ee"=>"teseweq222ewt");
//$cache->set_cache($cache_file, $data);
//print_r($cache->get_cache($cache_file));
?>
[/code]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值