PHP Data caching Technology _php tutorial

Source: Internet
Author: User
Tags qoute
Data caching is a kind of performance optimization method commonly used in web development. At present, the main file cache or database cache two forms, database cache database is not what is impossible, it is also very good is very important. I think the traditional database is mainly from the business layer, module design and other aspects to consider, and the cache database is mainly from the implementation layer to design, mainly in order to cache common multi-table query and so on. Here the main is the file cache, a lot of information on the Web, here I reproduced some of the principles of data.
Cache is a typical application mode of "space-time" strategy, which is an important method to improve the performance of the system. The use of the cache can greatly reduce the number of database operations and significantly reduce the system load and improve the performance of the system under the condition of large traffic. Compared to the cache of the page, the result set is a "raw data" that does not contain formatting information, the amount of data is relatively small, and can be formatted again, so it seems quite flexible. Because PHP is a scripting language that "executes on one side", it also provides a fairly handy way of using the result set cache-using caching in a way that dynamically includes the corresponding data definition snippet. If the cache is built on "RamDisk", the efficiency should be improved further. The following is a short sample code for reference.

//Load data with cache

functionLoad_data($id,$cache _lifetime) {

//The return data

$data= Array ();

//Make cache filename

$cache _filename= ' cache_ '.$id.'.php ';

//Check cache file ' s last modify time

$cache _filetime= Filemtime($cache _filename);

if ( Time() - $cache _filetime<=$cache _lifetime) {

//** The cache is not expire

include ($cache _filename);

} else {

* * The cache is expired

Load data from Database

// ...

While ($dbo -NextRecord()) {

//$data [] = ...

}

//Format the data as a PHP file

$data _cache= "

while (list ($key, $val) = each ($data)) {

$data _cache. = "$data[' $key ']=array ('";

$data _cache. = "' NAME '="".Qoute($val[' NAME '])."\","

$data _cache.= " ' VALUE ' =>\" ".Qoute($val[' VALUE '])."\""

$data _cache.= ";); \ r \ n";

}

$data _cache= "? >\r\n";

Save the data to the cache file

if ($FD= fopen($cache _filename,' W+')) {

fputs($FD,$data _cache);

fclose($FD);

}

}

Return$data;

}

?>


Applicable situation:
1. The data is relatively stable, mainly read operation.
2. File operations are faster than database operations.
3. Complex data access, large data volume access, dense data access, system database load is very heavy.
4.WEB/DB separate structure or multi-Web single-DB structure.

Unconfirmed questions:
1. Whether read and write to the file during concurrent access can cause locking problems.
2. The data files involved are too long, how much performance.
Expansion ideas:
1. Generate JavaScript Data definition code that is called on the client.
2. Not yet thought ...

Hope to explore together.

http://www.bkjia.com/PHPjc/317934.html www.bkjia.com true http://www.bkjia.com/PHPjc/317934.html techarticle data caching is a kind of performance optimization method commonly used in web development. At present, the main file cache or database cache two forms, database cache database is not what is impossible, the ...

  • 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.