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.