PHP Program cache file Cache processing method
During the development process, the cache settings greatly increase program efficiency and reduce database load.
Basic configuration cache and general configuration cache
The basic configuration cache is formatted like this in project development:
Files: config.php
$CFG [' database '] = ' mysql '; $CFG [' pconnect '] = ' 0 '; $CFG [' db_host '] = ' localhost '; $CFG [' db_name '] = ' appcom '; $CFG [' db_user '] = ' root '; $CFG [' db_pass '] = ' 123456 '; $CFG [' db_charset '] = ' UTF8 ';
The general configuration cache is similar to this: for example, File x.php
return Array (' a ' + = ', ' b ' = = ', ' c ' = = ', ' d ' = = ', ' aa ' = = 'array(' x ' = = ', ' y ' =>, ' z ' = = '), ' BB ' = =array(' x ' = = ', ' y ' =>, ' z ' = = '), ' cc ' = =array(' x ' = = ', ' y ' =>, ' z ' = ' ") ));
Cache Configuration Program code:
Update for basic configuration:
$tmp = file_get (' config.inc.php '); foreach ($settingas$k= +$v) {$tmppreg_replace(" /[$]cfg\['$k' \]\s*\=\s*[\ "'].*? [\ "']/is '," \ $CFG ['$k'] = '$v$tmp);
General configuration updates: For example, the file name is y.php
Cache_write (' y.php ',$setting);functionCache_write ($file,$string,$dir= ' ') { if(Is_array($string))$string= "<?php defined (' In_kele ') or exit (' Access Denied '); Return ". STRIP_NR (Var_export($string,true))."; ? > "; $file=$file; $strlen= File_put ($file,$string); return $strlen;}functionSTRIP_NR ($string,$js=false) { $string=Str_replace(Array(CHR(13),CHR(ten), "\ n", "\ r", "\ T", "'),Array(‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘),$string); if($js)$string=Str_replace("‘", "\‘",$string); return $string;}functionFile_put ($filename,$data) {$phpServer=Strpos(Strtoupper(Php_os), ' WIN ')!==false?true:false; Dir_create (dirname($filename)); if(@$fp=fopen($filename, ' WB ')) { Flock($fp,lock_ex); $len=fwrite($fp,$data); Flock($fp,Lock_un); fclose($fp); if($phpServer) @chmod($filename,$phpServer); return $len; } Else { return false; }}functionFile_get ($filename) { return@file_get_contents($filename);}