溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用php怎么書寫一個(gè)帶緩存數(shù)據(jù)功能的mysqli類

發(fā)布時(shí)間:2020-12-21 15:53:28 來源:億速云 閱讀:130 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)利用php怎么書寫一個(gè)帶緩存數(shù)據(jù)功能的mysqli類,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

復(fù)制代碼 代碼如下:


<?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 '<h3>'.mysqli_connect_error().'</h3>';
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 "<h3>".$this->mysqli->error."</h3>";
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怎么書寫一個(gè)帶緩存數(shù)據(jù)功能的mysqli類了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI