PHP程式緩衝之檔案快取處理方式

來源:互聯網
上載者:User

標籤:style   blog   color   os   檔案   資料   

PHP程式緩衝之檔案快取處理方式
在開發程式過程中,緩衝的設定大大提升程式效率,減小資料庫負載。
基本配置緩衝和常規配置緩衝

基本配置緩衝在項目開發中類似這樣子的格式:

檔案: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‘;

常規配置緩衝類似這樣的:例如檔案x.php

return array (‘a‘=>‘‘,‘b‘=>‘‘,‘c‘=>‘‘,‘d‘=>‘‘,‘aa‘=>array(‘x‘=>‘‘,‘y‘=>,‘z‘=>‘‘),‘bb‘=>array(‘x‘=>‘‘,‘y‘=>,‘z‘=>‘‘),‘cc‘=>array(‘x‘=>‘‘,‘y‘=>,‘z‘=>‘‘));

緩衝配置程式碼:
基本配置的更新:

$tmp = file_get(‘config.inc.php‘);foreach($setting as $k=>$v) {            $tmp = preg_replace("/[$]CFG\[‘$k‘\]\s*\=\s*[\"‘].*?[\"‘]/is", "\$CFG[‘$k‘] = ‘$v‘", $tmp);

常規配置的更新:例如檔案名稱為y.php

cache_write(‘y.php‘, $setting);function cache_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;}function strip_nr($string, $js = false) {    $string =  str_replace(array(chr(13), chr(10), "\n", "\r", "\t", ‘  ‘),array(‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘), $string);    if($js) $string = str_replace("‘", "\‘", $string);    return $string;}function file_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;    }}function file_get($filename) {    return @file_get_contents($filename);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.