Database caching principle in PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:07:02
Original
732 people have browsed it

The author of this article mainly uses the database cache in php, php cache, cache_write, and var_export functions to cache data, and I think the writing is very good.

Database caching principle in PHP

The author of this article mainly uses the database cache in php, php cache, cache_write, and var_export functions to cache data, and I think the writing is very good.


If the background application receives a query request from the browser and connects to the database to read data every time, it will inevitably increase the burden on the database. There are often a large number of repeated requests. We can use caching technology to save these repeated information and reuse them. In this way, in some cases, the performance of the program can be greatly improved.
1. Cache function
The cache_write function accepts the $string parameter and writes it to the $file file. Pay attention to the var_export function, its function is:
This function returns structural information about the variables passed to the function. It is similar to var_dump(), except that the representation returned is legal PHP code. You can return a representation of a variable by setting the function's second argument to TRUE.
These parameters can be arrays or constants, and these arrays or constants are usually records taken from the database, or data obtained after unserializing the object. These can be cached in local text files.
The cache_write function is very simple. When you need to read data, first determine whether the cache exists. If it exists, it will not connect to the database to get the data. Instead, it will directly read the cached text file and directly generate data of arrays or constants. You can directly use.
[php]
//File name func.inc.php
define("CACHEDIR", "./"); //Define cache folder
function cache_write($file, $string, $type = 'array')
{
If(is_array($string))
{
$type = strtolower($type);
If($type == 'array')
{
$string = "";
}
​ elseif($type == 'constant')
{
$data='';
foreach($string as $key => $value)
          $data .= "define('".strtoupper($key)."','".addslashes($value)."');n";
$string = "";
}
}
$strlen = file_put_contents(CACHEDIR.$file, $string);
chmod(CACHEDIR.$file, 0777);
Return $strlen;
}
function cache_read($file)
{
$cachefile = CACHEDIR.$file;
If(!file_exists($cachefile))
Return array();
Return include $cachefile;
}
function cache_delete($file)
{
Return @unlink(CACHEDIR.$file);
}
if(!function_exists('file_put_contents'))
{
define('FILE_APPEND', 8);
Function file_put_contents($file, $string, $append = '')
{
$mode = $append == '' ? 'wb' : 'ab';
$fp = @fopen($file, $mode) or exit("Can not open file $file !");
flock($fp, LOCK_EX);
$stringlen = @fwrite($fp, $string);
flock($fp, LOCK_UN);
@fclose($fp);
Return $stringlen;
}
}
?>
[/php]
2. Examples of write caching and reading
[php]
​​ ​​​​ //Write cache
  include "func.inc.php";

​​ $arr = array (1, 2, array ("a", "b", "c"));
​ cache_write('test.cache.php', $arr); //Cache file test.cache.php
?>
[/php]
[php]
​​ //Read cache
  include "func.inc.php";

​ ​ $var = cache_read('test.cache.php');
​​ print_r($new_var);

​​ print_r($var);

​ ​ foreach ($var as $k=>$v)
​​{
echo '
' . $k . '=' . $v ;
  }
?>
[/php]
3. Performance analysis
The reason why caching can improve performance is to exchange local disk space for network access speed and database server access time.
a = local read and write time
b = space occupied by this machine
c = network transmission time
d = database server disk time
It can be estimated that if the database and application exist on one machine, the main comparison between a and d will not be obvious, or even worse. Because the database system has been carefully optimized for disk access, it is incomparable to the operating system's ordinary reading and writing of files.
If the disk access efficiency of the local machine is not good, sometimes retrieving data from the database on the LAN may be faster than retrieving data from the cache of the local machine. This situation is relatively rare. As the number of requests increases significantly, the effect of caching will become obvious.
I think it’s very good, so I recommend it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630479.htmlTechArticleThe author of this article mainly uses the database cache, php cache, cache_write, and var_export functions in php to cache data. After using it, I think the writing is very good. Database caching in PHP...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!