午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    phpcms緩存使用總結(jié)(memcached、eaccelerator、shm)
    來源:易賢網(wǎng) 閱讀:845 次 日期:2014-08-27 15:38:44
    溫馨提示:易賢網(wǎng)小編為您整理了“phpcms緩存使用總結(jié)(memcached、eaccelerator、shm)”,方便廣大網(wǎng)友查閱!

    a.模板編譯緩存

    參考文件include/global.func.php及include/template.func.php

    模 板編譯緩存的原理其實很簡單,如果模板是第一次編譯,則直接編譯它,如果不是第一次編譯,則比較模板文件($tplfile)及模板緩存文件 ($compiledtplfile)的修改時間,如果模板文件的修改時間大于編譯過的模板緩存文件,則編譯模板,否則不編譯模板,提高了程序的執(zhí)行效 率。

    代碼如下:

    function template($module = 'phpcms', $template = 'index')

    {

    global $CONFIG;

    $compiledtplfile = $CONFIG['templatescachedir'].$module.'_'.$template.'.tpl.php';

    if($CONFIG['templaterefresh'])

    {

    $tplfile = PHPCMS_ROOT.'/templates/'.$CONFIG['defaulttemplate'].'/'.$module.'/'.$template.'.html';

    if(!file_exists($compiledtplfile) || @filemtime($tplfile) > @filemtime($compiledtplfile))

    {

    require_once PHPCMS_ROOT.'/include/template.func.php';

    template_refresh($tplfile, $compiledtplfile);

    }

    }

    return $compiledtplfile;

    }

    b.在動態(tài)頁面里面產(chǎn)生靜態(tài)的緩存文件

    與c的緩存原理類似,只是此處生成的文件名相對固定

    以問吧模塊為例進行說明

    用http://www.chf.com/opensource/phpcms2007_sp6_gbk/phpcms/wenba/進行訪問

    此目錄下有個index.php文件,判斷當(dāng)前目錄下是否存在名為index_cache.html的文件,如果有沒有過失效期,則直接包含此文件,否則動態(tài)地讀取完數(shù)據(jù)后保存為index_cache.html文件,以備下次使用。

    文件index.php中的內(nèi)容:

    代碼如下:

    <?php

    require_once './include/common.inc.php';

    $lastedittime = @filemtime('index_cache.html');

    $lastedittime = $PHP_TIME-$lastedittime;

    $autoupdatetime = intval($MOD['autoupdate']); //$MOD['autoupdate']來自緩存文件data/cache/wenba_setting.php中的內(nèi)容

    if(file_exists('index_cache.html') && $lastedittime<$autoupdatetime)

    {

    echo "include cache file";

    include 'index_cache.html';

    }

    else

    {

    echo "read dynamic page";

    ...

    ?>

    怎么判斷文件是否失效呢,文件data/cache/wenba_setting.php中有如下的設(shè)置,其中字段autoupdate的值就是文件失效的時間,單位是秒,在后臺可以進行設(shè)置

    文件wenba_setting.php是從哪兒來的呢,進行安裝時自動把各種模塊的數(shù)據(jù)保存到數(shù)據(jù)庫中了,安裝時就生成緩存數(shù)據(jù)了,在include/common.inc.php中函數(shù)cache_all也可以生成緩存,后臺進行設(shè)置時cache會自動更新的

    代碼如下:

    <?php

    return array (

    'higth_score' => '100',

    'anybody_score' => '2',

    'answer_give_credit' => '5',

    'vote_give_credit' => '1',

    'highscore' => '2',

    'vote_give_actor' => '公司白領(lǐng)</p> <p>魔法師</p> <p>科舉奪魁</p> <p>武將</p> <p>江湖奇?zhèn)b',

    'autoupdate' => '10',

    'name' => '問吧',

    'moduledir' => 'wenba',

    'moduledomain' => '',

    'linkurl' => '/opensource/phpcms2007_sp6_gbk/phpcms/wenba/',

    );

    ?>

    include/global.func.php

    更新模塊設(shè)置函數(shù)

    代碼如下:

    function module_setting($module, $setting)

    {

    global $db,$MODULE,$LANG;

    if(!is_array($setting) || !array_key_exists($module,$MODULE)) return FALSE;

    if(isset($setting['moduledomain']))

    {

    $moduledomain = $setting['moduledomain'];

    $db->query("UPDATE ".TABLE_MODULE." SET moduledomain='$moduledomain' WHERE module='$module'");

    unset($setting['moduledomain']);

    }

    $setting = addslashes(serialize(new_stripslashes($setting)));

    //將某個模塊的多個設(shè)置的值經(jīng)數(shù)組序列化以后保存在一個字段setting中

    $db->query("UPDATE ".TABLE_MODULE." SET setting='$setting' WHERE module='$module'");

    cache_module($module);

    cache_common();

    return TRUE;

    }

    c.在動態(tài)頁面里面產(chǎn)生靜態(tài)的緩存文件

    與b的緩存原理類似,只是此處生成的文件名是根據(jù)計算$PHP_SELF與$PHP_QUERYSTRING的md5值生成的文件名,相對于所有php動態(tài)頁面來說都是一樣的,這個思想比較精典,值得借簽

    以問吧模塊為例進行說明

    文件調(diào)用順序為:index.php -> js.php -> ad.php -> global.func.php

    用http://www.chf.com/opensource/phpcms2007_sp6_gbk/phpcms/wenba/進行訪問

    此目錄下有個index.php文件,判斷當(dāng)前目錄下是否存在名為index_cache.html的文件,如果有,則直接包含此文件,如果不存在此文件,則動態(tài)地讀取完數(shù)據(jù)后保存在index_cache.html文件,以備下次使用

    用上述的url訪問時,頁面里面包含有如下的一行js代碼

    <script language="javascript" src="/opensource/phpcms2007_sp6_gbk/phpcms/data/js.php?id=1"></script>

    此js代碼其實就是動態(tài)調(diào)用php頁面的內(nèi)容

    http://www.chf.com/opensource/phpcms2007_sp6_gbk/phpcms/data/js.php?id=1

    js.php文件的內(nèi)容:

    代碼如下:

    <?php

    chdir('../ads/');

    require './ad.php';

    ?>

    ad.php的內(nèi)容:

    代碼如下:

    <?php

    define('SHOWJS', 1);

    require './include/common.inc.php';

    require MOD_ROOT.'/include/global.func.php';</p> <p>$placeid = intval($id);</p> <p>$query ="SELECT * FROM ".TABLE_ADS." AS a LEFT JOIN ".TABLE_ADS_PLACE." AS p ON (a.placeid=p.placeid) WHERE a.placeid=".$placeid." AND a.fromdate<=UNIX_TIMESTAMP() AND a.todate>=UNIX_TIMESTAMP() AND p.passed=1 AND a.passed=1 AND a.checked=1 ORDER BY a.addtime";

    $ads = $db->get_one($query, "CAHCE", 10240);

    if(!$ads) exit('document.write("")');</p> <p>$db->query("UPDATE ".TABLE_ADS." SET views=views+1 WHERE adsid=".$ads['adsid']);</p> <p>$content = ads_content($ads);

    $templateid = $ads['templateid'] ? $ads['templateid'] : 'ads';

    include template('ads', $templateid);

    phpcache();

    ?>

    ad.php里面調(diào)用了phpcache函數(shù),參考文件include/global.func.php

    代碼如下:

    function phpcache($is_js = 0)

    {

    global $CONFIG,$cachefiledir,$cachefile;

    if(!$is_js && $CONFIG['phpcache'] != '2') return FALSE;

    $contents = ob_get_clean(); //讀取緩沖區(qū)里面的內(nèi)容

    if($is_js) $contents = strip_js($contents);

    if($CONFIG['phpcache'] == '2' && $cachefiledir && $cachefile)

    {

    dir_create($cachefiledir);

    file_put_contents($cachefile, $contents); //在這兒生成一個.html格式的文件,當(dāng)下次以同樣的url訪問時,會直接讀取緩存了,參見include/common.inc.php中的代碼, 這兒的代碼是非常非常精典的,大家好好借鑒、好好模仿吧

    @chmod($cachefile, 0777);

    }

    /*

    向瀏覽器發(fā)送http header,跟瀏覽器說,此頁面不緩存,還告訴瀏覽器頁面的最后修改時間

    第一次訪問js.php?id=1時向瀏覽器發(fā)送http header,第二次或以后再訪問此url時,由于上次已經(jīng)生成了緩存,所以在include/common.inc.php中直接調(diào)用緩存文件了,直到 緩存失效后再次執(zhí)行此處的動態(tài)代碼。此處發(fā)送的header控制緩存是相對于瀏覽器來說的;而通過file_put_contents生成的緩存是相對于 電腦硬盤來說的,是不一樣的。

    */

    header('Expires: Mon, 26 Jul 2000 05:00:00 GMT');

    header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');

    header('Cache-Control: no-cache, must-revalidate');

    header('Pragma: no-cache');

    echo $contents;

    }

    上面的phpcache函數(shù)中的全局變量$cachefiledir,$cachefile是從哪里來的呢,從這兒來的

    文件include/common.inc.php中的內(nèi)容

    代碼如下:

    if(!defined('IN_ADMIN'))

    {

    if($CONFIG['dbiscache']) $db_file .= '_cache';

    if($CONFIG['phpcache'] == '2')

    {

    $cachefileid = md5($PHP_SELF.'?'.$PHP_QUERYSTRING);

    $cachefiledir = PHPCMS_ROOT.'/data/phpcache/'.substr($cachefileid, 0, 2).'/';

    $cachefile = $cachefiledir.$cachefileid.'.html';

    //echo "cachefile:$cachefile";

    if(file_exists($cachefile) && ($PHP_TIME < @filemtime($cachefile) + $CONFIG['phpcacheexpires']))

    {

    require $cachefile;

    exit;

    }

    }

    if($PHP_QUERYSTRING && preg_match("/^(.*).(htm|html|shtm|shtml)$/", $PHP_QUERYSTRING, $urlvar))

    {

    parse_str(str_replace(array('/', '-', ' '), array('&', '=', ''), $urlvar[1]));

    }

    }

    d.數(shù)據(jù)庫查詢結(jié)果緩存

    下面是include/common.inc.php中的幾行代碼

    代碼如下:

    $db_file = $db_class = 'db_'.$CONFIG['database']; //$CONFIG['database']位于config.inc.php中,配置可以使用自己的數(shù)據(jù)庫,如mysql,sqlite,sqlserver等

    require PHPCMS_ROOT.'/include/'.$db_file.'.class.php';

    $db = new $db_class;

    $db->connect($CONFIG['dbhost'], $CONFIG['dbuser'], $CONFIG['dbpw'], $CONFIG['dbname'], $CONFIG['pconnect']);

    $db->iscache = $CONFIG['dbiscache']; //是否啟用 sql cache (只對前臺起作用,建議在不生成html并且訪問量過大時開啟)

    $db->expires = $CONFIG['dbexpires']; //sql cache 過期時間(秒)

    db_mysql_cache.class.php中的代碼

    代碼如下:

    function query($sql , $type = '' , $expires = 3600, $dbname = '')

    {

    if($this->isclient)

    {

    $dbname = $dbname ? $dbname : $this->dbname;

    $this->select_db($dbname);

    }

    /*

    $this->iscache表示是否啟動了數(shù)據(jù)庫查詢緩存

    如果啟用了數(shù)據(jù)庫查詢緩存且$type為CACHE且是select語句,則啟用查詢緩存

    個人感覺這兒$type參數(shù)用strtoupper處理一下更好了

    */

    if($this->iscache && $type == 'CACHE' && stristr($sql, 'SELECT'))

    {

    $this->caching = 1; //成員變量caching標(biāo)識啟用了數(shù)據(jù)庫查詢緩存,用在下面的fetch_array,num_rows,free_result函數(shù)中,其實用 iscache就可以判斷了,沒有必要再用一個成員變量了

    $this->expires = $expires; //數(shù)據(jù)庫緩存數(shù)據(jù)的失效期

    return $this->_query_cache($sql); //然后調(diào)用_query_cache方法

    }

    $this->caching = 0;

    $func = $type == 'UNBUFFERED' ? 'mysql_unbuffered_query' : 'mysql_query';

    if(!($query = $func($sql , $this->connid)) && $type != 'SILENT')

    {

    $this->halt('MySQL Query Error', $sql);

    }

    $this->querynum++;

    return $query;

    }</p> <p>function _query_cache($sql)

    {

    $this->cache_id = md5($sql); //計算$sql的md5值,然后作為cache_id

    $this->result = array();

    $this->cursor = 0;

    $this->cache_file = $this->_get_file(); //得到cache文件名

    //如果cache數(shù)據(jù)已經(jīng)過期,則重新從數(shù)據(jù)庫中取得查詢結(jié)果,然后保存在數(shù)據(jù)庫中

    if($this->_is_expire())

    {

    $this->result = $this->_get_array($sql); //從數(shù)據(jù)庫中取結(jié)果

    $this->_save_result(); //保存結(jié)果到緩存數(shù)據(jù)中

    }

    else

    {

    $this->result = $this->_get_result(); //緩存沒過期直接取緩存數(shù)據(jù)

    }

    return $this->result;

    }</p> <p> function _get_file()

    {

    global $CONFIG;

    //cache文件的主目錄一般是data/dbcache

    return $CONFIG['dbcachedir'].substr($this->cache_id, 0, 2).'/'.$this->cache_id.'.php';

    }</p> <p>function _is_expire()

    {

    global $PHP_TIME;

    return !file_exists($this->cache_file) || ( $PHP_TIME > @filemtime($this->cache_file) + $this->expires );

    }</p> <p>/*

    由于方法_get_array只是被方法_query_cache調(diào)用,所以在此方法里面直接用函數(shù)mysql_unbuffered_query了,因為mysql_unbuffered性能好一點,參考

    <a >http://bbs.chinaunix.net/viewthread.php?tid=958067&extra=page%3D4</a>

    */

    function _get_array($sql)

    {

    $this->cursor = 0;

    $arr = array();

    $result = mysql_unbuffered_query($sql, $this->connid);

    while($row = mysql_fetch_assoc($result))

    {

    $arr[] = $row;

    }

    $this->free_result($result);

    $this->querynum++;

    return $arr;

    }</p> <p>function _save_result()

    {

    if(!is_array($this->result)) return FALSE;

    dir_create(dirname($this->cache_file));

    file_put_contents($this->cache_file, "<?phpn return ".var_export($this->result, TRUE).";n?>");

    @chmod($this->cache_file, 0777);

    }</p> <p> function _get_result()

    {

    return include $this->cache_file;

    }</p> <p>function fetch_array($query, $result_type = MYSQL_ASSOC)

    {

    return $this->caching ? $this->_fetch_array($query) : mysql_fetch_array($query, $result_type);

    }</p> <p>//從數(shù)據(jù)庫中獲取查詢的結(jié)果

    function _fetch_array($result = array())

    {

    if($result) $this->result = $result;

    return isset($this->result[$this->cursor]) ? $this->result[$this->cursor++] : FALSE;

    }</p> <p>function num_rows($query)

    {

    return $this->caching ? $this->_num_rows($query) : mysql_num_rows($query);

    }</p> <p>function free_result($query)

    {

    if($this->caching==1) $this->result = array();

    else @mysql_free_result($query);

    }

    如果把上述的文件存儲改為用memcached、eaccelerator、shm等來進行存儲的話效率會更高,改動起來也不是太難,后臺可以加一個設(shè)置選項,如分別是

    文件,memcached,eaccelerator,shm等讓管理員進行設(shè)置,然后調(diào)用相應(yīng)的存儲系統(tǒng)進行存儲

    更多信息請查看IT技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程

    2025國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)