Home Backend Development PHP Tutorial A mysqli class with cache data function_PHP tutorial

A mysqli class with cache data function_PHP tutorial

Jul 13, 2016 pm 05:05 PM
mysqli under share Function bring data have of kind cache

Let’s share a mysqli class with cache data function. Students in need can take a look.

The code is as follows Copy code

/**
* Mysqli class
* *
* @author ruins
* @version v1.0 2009-08-18

    */  
    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';//若框架中使用__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(),'
';
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630729.htmlTechArticleThe following is a mysqli class with cache data function. Students in need can take a look. The code is as follows Copy code ?php /** * Mysqli class * * @author ruins * @version v1.0 2009-08-18...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Apr 03, 2024 pm 12:04 PM

0.What does this article do? We propose DepthFM: a versatile and fast state-of-the-art generative monocular depth estimation model. In addition to traditional depth estimation tasks, DepthFM also demonstrates state-of-the-art capabilities in downstream tasks such as depth inpainting. DepthFM is efficient and can synthesize depth maps within a few inference steps. Let’s read about this work together ~ 1. Paper information title: DepthFM: FastMonocularDepthEstimationwithFlowMatching Author: MingGui, JohannesS.Fischer, UlrichPrestel, PingchuanMa, Dmytr

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Apr 01, 2024 pm 07:46 PM

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

I cry to death. The world is madly building big models. The data on the Internet is not enough. It is not enough at all. The training model looks like "The Hunger Games", and AI researchers around the world are worrying about how to feed these data voracious eaters. This problem is particularly prominent in multi-modal tasks. At a time when nothing could be done, a start-up team from the Department of Renmin University of China used its own new model to become the first in China to make "model-generated data feed itself" a reality. Moreover, it is a two-pronged approach on the understanding side and the generation side. Both sides can generate high-quality, multi-modal new data and provide data feedback to the model itself. What is a model? Awaker 1.0, a large multi-modal model that just appeared on the Zhongguancun Forum. Who is the team? Sophon engine. Founded by Gao Yizhao, a doctoral student at Renmin University’s Hillhouse School of Artificial Intelligence.

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

Recently, the military circle has been overwhelmed by the news: US military fighter jets can now complete fully automatic air combat using AI. Yes, just recently, the US military’s AI fighter jet was made public for the first time and the mystery was unveiled. The full name of this fighter is the Variable Stability Simulator Test Aircraft (VISTA). It was personally flown by the Secretary of the US Air Force to simulate a one-on-one air battle. On May 2, U.S. Air Force Secretary Frank Kendall took off in an X-62AVISTA at Edwards Air Force Base. Note that during the one-hour flight, all flight actions were completed autonomously by AI! Kendall said - "For the past few decades, we have been thinking about the unlimited potential of autonomous air-to-air combat, but it has always seemed out of reach." However now,

Alibaba 7B multi-modal document understanding large model wins new SOTA Alibaba 7B multi-modal document understanding large model wins new SOTA Apr 02, 2024 am 11:31 AM

New SOTA for multimodal document understanding capabilities! Alibaba's mPLUG team released the latest open source work mPLUG-DocOwl1.5, which proposed a series of solutions to address the four major challenges of high-resolution image text recognition, general document structure understanding, instruction following, and introduction of external knowledge. Without further ado, let’s look at the effects first. One-click recognition and conversion of charts with complex structures into Markdown format: Charts of different styles are available: More detailed text recognition and positioning can also be easily handled: Detailed explanations of document understanding can also be given: You know, "Document Understanding" is currently An important scenario for the implementation of large language models. There are many products on the market to assist document reading. Some of them mainly use OCR systems for text recognition and cooperate with LLM for text processing.

Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! May 06, 2024 pm 04:13 PM

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

2024 QS ranking released! Computer science MIT dominates the list, Tsinghua University is 11th, Peking University is 15th 2024 QS ranking released! Computer science MIT dominates the list, Tsinghua University is 11th, Peking University is 15th Apr 18, 2024 pm 09:04 PM

The 2024QS World University Rankings by Subject is here! Overall, there is little change from 2023. According to the official website information, the 2024QS World University Rankings by Subject covers 55 subdivisions and 5 major academic fields. A total of 1,559 universities participated in the ranking, 64 of which are new faces this year (that is, they will not appear in the 2023 ranking). Among these 64 colleges and universities, 14 are truly appearing for the first time. Among them is the University of Chinese Academy of Sciences. According to the refined subjects, Music is a new subject introduced this year. In addition, the data science and artificial intelligence rankings have been expanded, with 51 new universities added to the rankings. The top five in the overall list are: Massachusetts Institute of Technology, University of Cambridge, University of Oxford, and Harvard University

See all articles