Principles of Database caching in PHP

Source: Internet
Author: User
Tags foreach arrays constant flock php code strlen

Principles of Database caching in PHP


If the background application reads data from the database every time it receives a browser query request, it will inevitably increase the burden on the database. There are often a large number of requests that are repeated. We can save the duplicate information using the cache technology and reuse it. In this way, the program performance can be greatly improved in some cases.
1. Cache functions
The cache_write function accepts the $ string parameter and writes it to the $ file. Note that the var_export function is used:
This function returns the structure information about the variables passed to this function. It is similar to var_dump (). The difference is that the returned representation is legal PHP code. You can set the second parameter of the function to TRUE to return the expression of the variable.
These parameters can be arrays or constants, and these arrays or constants are usually the records retrieved from the database or the data obtained after the unserialize object. These can be cached in local text files.
The cache_write function is very simple. When you need to read data, you must first determine whether the cache exists. If so, you do not connect to the database to retrieve data, but directly read the cached text file, directly generate data of the array or constant type, which can be directly used.
[Php]
<? Php
// File name func. inc. php
Define ("CACHEDIR", "./"); // defines the cache folder
Function cache_write ($ file, $ string, $ type = 'array ')
  {
If (is_array ($ string ))
   {
$ Type = strtolower ($ type );
If ($ type = 'array ')
    {
$ String = "<? Phpn return ". var_export ($ string, TRUE)."; n?> ";
    }
Elseif ($ type = 'constant ')
    {
$ Data = '';
Foreach ($ string as $ key => $ value)
$ Data. = "define ('". strtoupper ($ key). "', '". addslashes ($ value). "'); n ";
$ String = "<? Phpn ". $ data." n?> ";
    }
   }
$ 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. Example of writing cache and reading
[Php]
<? 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]
<? 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 '<br>'. $ k. '='. $ v;
}
?>
[/Php]
3. Performance analysis
The reason why the cache improves performance is the result of changing the network access speed and database server access time through the local disk space.
A = local read/write time
B = space occupied by the local 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 comparison between a and d may not be obvious, or even worse. Because the database system has been carefully optimized for disk access, it is impossible for the operating system to perform normal read/write operations on files.
If the local disk access efficiency is poor, sometimes the data obtained from the local database on the LAN may be faster than the data obtained from the local cache, which is rare. As the number of requests increases, the cache effect becomes obvious.
I think it is good, so I recommend it.

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.