PHP Data caching Technology _php skills

Source: Internet
Author: User
Tags qoute
Data caching is one of the most common performance optimization methods in web development. At present, the main file cache or database cache two forms, database caching database is not an impossible thing, is indeed very good and important. I think that 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 commonly used multiple table query and so on. Here is the main file cache, a lot of information on the Internet, here I reproduced some of the principles of information.
Cache is a typical application mode of "Space Change Time" strategy, and it is an important method to improve system performance. The use of caching can greatly reduce the number of database operations and significantly reduce the system load and improve system performance with large traffic. Compared to the page cache, the result set is a "raw data" does not contain format information, the amount of data is relatively small, and can be formatted, so it appears quite flexible. Because PHP is a scripting language that executes on one side, it also provides a fairly convenient method of result set caching-using caching in a way that dynamically include the corresponding data definition snippet. If you build a cache on "RamDisk", the efficiency should be further improved. 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 (the 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 for reading operations.
2. File operations are faster than database operations.
3. Complex data access, large amount of data access, intensive data access, system database load is very heavy.
4.WEB/DB separation structure or multiple WEB single DB architecture.

Unconfirmed questions:
1. Whether read and write to the file during concurrent access can cause a lock problem.
2. The data file involved is too long and the performance is very good.
Expanding ideas:
1. Generate JavaScript data definition code, called on the client.
2. It has not been thought that ...

Hope to discuss together.

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.