Analysis of common cache technology instances in PHP

Source: Internet
Author: User
This article mainly introduces common cache technologies in PHP, and analyzes in detail the principles, characteristics, and usage skills of the cache technology in php in the form of examples, which has some reference value, you can refer to the examples in this article to analyze common cache technologies in PHP. Share it with you for your reference. The details are as follows:

During the development process, JBLOG has done a lot of work on performance optimization. In order to minimize unnecessary database queries, I cache and statically process some data.

Cache principle: stores frequently used but rarely changed data in an array or other form into an independent php file, and then includes it when necessary.

Cache advantages: it can greatly reduce the number of database queries, reduce the pressure on the database, and improve the program execution efficiency.

JBLOG cache data includes: system settings, blog categories, latest sidebar logs, latest comments, blog statistics, log archiving, links, tags, etc. By caching the data, the number of queries performed on a page database is reduced from 10 to 3.

Cache-related functions in JBLOG are stored in cache. func. php under the include directory. main functions:

// Refresh the cache function recache ($ cachestr = '') {if (! $ Cachestr) {$ cachelist = array ('config', 'class', 'archive', 'newcomment', 'newpost', 'link', 'tag ', 'statistic ', 'topblog');} else {$ cachelist = explode (',', $ cachestr); foreach ($ cachelist as $ cache) {$ cachename = $ cache. '_ recache'; if (function_exists ($ cachename) {$ cachename ();}}}}

The recache () function is used to refresh the cache. each cache exists as an independent function. you only need to execute the corresponding function once to refresh the cache.

// Write the string into the file function writeToFile ($ cachename, $ content = '') {$ allowcache = $ cachelist = array ('config', 'class', 'archive ', 'newcomment', 'newpost', 'link', 'tag', 'statistic ', 'topblog'); if (in_array ($ cachename, $ allowcache )) {$ cache_dir = JBLOG_ROOT. 'cache _ data/'; $ cache_file_name = $ cache_dir. 'cache _'. $ cachename. '. php'; if (! Is_dir ($ cache_dir) {@ mkdir ($ cache_dir, 0777);} if ($ fp = @ fopen ($ cache_file_name, 'WB ') {$ content = "<? Php \ r \ n // This file is a cache file automatically generated by the system. do not modify \ r \ n // creation time :". date ('Y-m-d H: I: S', time ()). "\ r \ n \ r \ nif (! Defined ('in _ JBLOG ') {exit ('Access Denied! ') ;}\ R \ n \ r \ n ". $ content." \ r \ n?> "; @ Fwrite ($ fp, $ content); @ fclose (); @ chmod ($ cache_file_name, 0777);} else {echo 'cache file'. $ Cache_dir. $ cache_file_name .'Creation failed!
';}} Else {die ('cache name'. $ Cachename .'Not in the scope permitted by the system! ');}}

The writeToFile () function is used to write data to a file named "cache _ cache name. php" in the cache directory.
Let's look at the specific cache function instance:

// Cache blog category function class_recache () {global $ db, $ tablepre; $ content = ''; $ SQL =" SELECT id, classname, description, orderid, arcnum FROM '{$ tablepre} class 'Order BY orderid "; $ result = $ db-> query ($ SQL ); while ($ row = $ db-> fetch_array ($ result) {$ content. = "\ tarray (\ r \ n"; $ content. = "\ t 'id' => '". addslashes ($ row ['id']). "', \ r \ n"; $ content. = "\ t 'classname' => '". addslashes ($ row ['classname']). "', \ r \ n"; $ content. = "\ t 'description' => '". addslashes ($ row ['description']). "', \ r \ n"; $ content. = "\ t 'orderid' => '". addslashes ($ row ['orderid']). "', \ r \ n"; $ content. = "\ t 'arcnum' => '". addslashes ($ row ['arcnum']). "', \ r \ n"; $ content. = "\ t), \ r \ n" ;}$ content = substr ($ content, 0, strrpos ($ content ,',')); $ content = "\ $ class_cache = array (\ r \ n {$ content} \ r \ n);"; writeToFile ('class', $ content );}

The class_recache () function extracts data from the database and constructs an array with the category ID as the index of the array and the category information as the corresponding value to facilitate data access.
Cache introduction:

All cached data is uniformly introduced in common. inc. php under the include directory. the code is as follows:

// Load system settings. if the file does not exist, the cache is rebuilt if ([email =! @ Include (JBLOG_ROOT.]! @ Include (JBLOG_ROOT. 'cache _ data/cache_config.php '[/email]) {require_once (JBLOG_ROOT. 'include/cache. func. php '); recache ('config'); exit ('system configuration information cache is created successfully. please refresh the page! ');} // Load the cache. if the cached file does not exist, rebuild the cache $ cachestr = ''; $ cachelist = array ('class', 'archive', 'newcomment ', 'newpost', 'link', 'tag', 'statistic ', 'topblog'); foreach ($ cachelist as $ cachename) {$ cachestr. = (@ include (JBLOG_ROOT. 'cache _ data/cache _'. $ cachename. '. php '))? '': $ Cachename. ',';} $ cachestr = substr ($ cachestr, 0, strrpos ($ cachestr, ','); if ($ cachestr) {require_once (JBLOG_ROOT. 'include/cache. func. php '); recache ($ cachestr); exit ('All cache reconstruction is complete. please refresh the page! ');} Unset ($ cachelist, $ cachename, $ cachestr );

The configuration information is loaded first because system settings are often used when other cached files are created. for example, the system settings have an option that allows you to customize the number of latest logs, this variable is used when the latest log is cached. Therefore, you must make sure that the configuration information is successfully cached before caching other items.

I hope this article will help you with php programming.

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.