下面来分享一个带缓存数据功能的mysqli类,有需要的同学可看看。
代码如下 | 复制代码 |
/** */ '.mysqli_connect_error().'';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 " ".$this->mysqli->error."";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(),' '; ?> |

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。