Common examples of caching techniques in PHP _php techniques

Source: Internet
Author: User

This article analyzes the common caching techniques in PHP. Share to everyone for your reference. Specifically as follows:

Jblog in the process of development, the performance of the optimization has done a lot of work. In order to minimize unnecessary database queries, I have cached and statically processed some of the data.

The principle of caching: storing data that is often used but rarely altered in an array or other form into a separate PHP file, and then include it when needed.

Advantages of caching: it can greatly reduce the number of database queries, reduce the pressure of the database, and improve the execution efficiency of the program.

Jblog cached data are: System settings, blog classification, the latest log of the sidebar, the latest comments, blog statistics, log archiving, links, labels and so on. By caching this data, the number of queries that perform a page database has been reduced from more than 10 to 3 times.

Cache-related functions in Jblog are stored in the cache.func.php in the Include directory, the main function:

Flush 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 flush the cache, and each cache exists as a separate function, and the cache needs to be refreshed with only one function to perform.

Writes a string to a 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//The file is a cached file that is automatically generated by the system, do not modify the \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\r\n?> ";
  @fwrite ($fp, $content);
  @fclose ();
  @chmod ($cache _file_name,0777);
 } else {
  echo ' cache file <b> '. $cache _dir. $cache _file_name. ' </b> creation failed! <br/> ';
 }
} else {
 die (' Cache name <b> '. $cachename. ' </b> not within the system allowed! ');
}
}

The WriteToFile () function is used to write data to the cached directory to cache_ the cached name. php named file.
Then look at the specific example of a cache function:

Cache Blog Classification
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 the data from the database, then constructs an array with the category ID as the index of the array, and the category's information as the corresponding value to facilitate access to the data.
Introduction of Caching:

All cached data is common.inc.php introduced in the Include directory, the code is as follows:

Load system setup Information and the file does not exist rebuilds the cache
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 (' successfully create the system configuration information cache, please refresh the page! ');
}
Load cache, cache file does not exist rebuild 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 rebuild complete, please refresh page! ');
}
Unset ($cachelist, $cachename, $CACHESTR);

The configuration information is loaded first because, when other cached files are created, often use the system settings information, such as the system setup has an option to allow users to customize the number of the latest log, in the cache when the latest log will use the variable, you must first ensure that the configuration information successfully cached, and then cache other items.

I hope this article will help you with your PHP program design.

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.