Asf PHP 開發之配置信息常駐系統內存

傳統MVC 與 Asf 比較

傳統的MVC框架

每次請求都會去從新加載配置文件。即便配置文件內容沒有更新, 也會去從新加載一次。這是一個很很差的設計。(開啓Opcache狀況下, 仍是有執行的過程時間)php

Asf框架

讀取到配置文件的內容保存到系統內存, 下一次請求直接去內存讀取數據。Asf 也提供很是簡單的配置實現 Config Cache。redis

什麼場景下開啓Config Cache合適?

  • 建議在Web應用場景下都開啓吧, 後面版本可能會默認啓用
  • 在CLI、多線程模式下開啓一樣生效, 只是PHP腳本每次執行完就釋放了
  • 支持數據類型有: strings, arrays, integers, boolean, doubles, floats, null

流程圖

avatar

開啓緩存方法

<?php
ini_set('asf.cache_config_enable', 1); /* 開啓配置文件緩存 */
ini_set('asf.cache_config_expire', 300); /* 設置緩存多少秒以後過時, 300 seconds by default */

框架入口方式加載php/ini配置文件

<?php
define('APP_PATH', dirname(__DIR__));

/* 緩存 config.ini 文件 */
$app = new Asf\Application(APP_PATH . '/config/config.ini');
$app->run();

Asf\Config\Php 加載php配置文件

<?php
$conf_php =  new Asf\Config\Php(CONFIG_PATH . '/config.db.php');

Asf\Config\Ini 加載ini配置文件

<?php
$conf_ini =  new Asf\Config\Ini(CONFIG_PATH . '/config.redis.ini');

讀取配置內容方法

<?php
print_r(Asf\Application::getInstance()->getConfig()->toArray());
print_r(Asf\Config::get()->toArray());

性能測試

在開啓 Opcache 狀況下, 簡單作了一個 Config Cache 性能測試, ab -c100 -n10000緩存

配置文件中配置項複雜程度與性能指標是有直線聯繫的喲多線程

開啓緩存 asf.cache_config_enable = 1

Total transferred:      16109994 bytes
HTML transferred:       14259994 bytes
Requests per second:    6859.01 [#/sec] (mean)
Time per request:       14.579 [ms] (mean)
Time per request:       0.146 [ms] (mean, across all concurrent requests)

無緩存

Total transferred:      16080000 bytes
HTML transferred:       14230000 bytes
Requests per second:    6398.22 [#/sec] (mean)
Time per request:       15.629 [ms] (mean)
Time per request:       0.156 [ms] (mean, across all concurrent requests)

提示

Cache Config 不是基於共享內存的, 是基於 PHP 進程的喲, 不會有共享內存鎖的問題。app

相關文章
相關標籤/搜索