This article shares a mysqli class with the cache data function, which is very useful
The code is as follows:
/**
* Mysqli class
*/
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 ''. 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'; // if _ autoload () is used in the framework, the file does not need to be loaded.
$ 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;
}
}
// Usage
$ 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 (),'
';
?>