Mysqli class with cache data written in php

Source: Internet
Author: User

Copy codeThe Code is as follows: <? Php
/**
* 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 '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 "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 (), '<br> ';
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.