PHP Program cache file Cache processing method

Source: Internet
Author: User
Tags flock

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);}

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.