検索
ホームページphp教程php手册PHPで書かれたキャッシュデータ機能を備えたMysqliクラス

PHPで書かれたキャッシュデータ機能を備えたMysqliクラス

Jun 13, 2016 am 11:58 AM
classmysqliphpコード関数コピー持ってくるデータ親切キャッシュ

复制代 代码如下:


/**
* Mysqli クラス
*/
class db_mysqli {
保護された $mysqli;
保護された $sql;
保護された $rs;
保護された $query_num = 0;
保護された $fetch_mode = MYSQLI_ASSOC;
保護された $cache_dir = './cache/';
保護された $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 '

'.mysqli_connect_error().'

';
死ぬ();
} else {
$this->mysqli->set_charset("utf8");
}
}
パブリック関数 __destruct() {
$this->free();
$this->close();
}
保護された関数 free() {
@$this->rs->free();
}
保護関数 close() {
$this->mysqli->close();
}
保護関数 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;
}
$sql を返す;
}
protected function get_cache($sql,$method) {
include_once './cache.php';//若框架中使用__autoload(),这里可以不使用追加ダウンロード文件
$キャッシュ = 新しいキャッシュ($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);
}
$res を返す;
}
パブリック関数 query_num() {
return $this->query_num;
}
public function set_cache_dir($cache_dir) {
$this->cache_dir = $cache_dir;
}
パブリック関数 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."

";
死ぬ();
} else {
$this->query_num ;
$this->rs を返す;
}
}
public function getOne($sql) {
$this->query($sql, 1);
$this->fetch_mode = MYSQLI_NUM;
$row = $this->fetch();
$this->free();
$row[0] を返す;
}
public function get_one($sql) {
return $this->getOne($sql);
}
パブリック関数cache_one($sql) {
$sql = $this->getQuerySql($sql, 1);
return $this->get_cache($sql, 'getOne');
}
パブリック関数 getRow($sql, $fetch_mode = MYSQLI_ASSOC) {
$this->query($sql, 1);
$this->fetch_mode = $fetch_mode;
$row = $this->fetch();
$this->free();
$row を返します;
}
public function get_row($sql, $fetch_mode = MYSQLI_ASSOC) {
return $this->getRow($sql);
}
パブリック関数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();
$all_rows を返します;
}
public function get_all($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
return $this->getAll($sql);
}
パブリック関数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 functionscape($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 = "単語 ID 制限 10,10 による単語順序から * を選択";
$res1 = $db->get_all($sql);
$res2 = $db->cache_all($sql);
echo $db->query_num(),'
';
?>
声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター