fanjiapeng
V2EX  ›  PHP

Asf PHP 开发之配置信息常驻系统内存

  •  1
     
  •   fanjiapeng · Jun 3, 2018 · 2459 views
    This topic created in 2905 days ago, the information mentioned may be changed or developed.

    传统的 MVC 框架

    每次请求都会去重新加载配置文件。即使配置文件内容没有更新, 也会去重新加载一次。这是一个很不好的设计。(开启 Opcache 情况下, 还是有执行的过程时间)

    Asf 框架

    读取到配置文件的内容保存到系统内存, 下一次请求直接去内存读取数据。Asf 也提供非常简单的配置实现 Config Cache。

    什么场景下开启 Config Cache 合适?

    • 建议在 Web 应用场景下都开启吧, 后面版本可能会默认启用

    • 在 CLI、多线程模式下开启同样生效, 只是 PHP 脚本每次执行完就释放了

    • 支持数据类型有: strings, arrays, integers, boolean, doubles, floats, null

    流程图

    流程图

    开启配置文件缓存技术

    ini_set('asf.cache_config_enable', 1); /* 开启配置文件缓存 */
    ini_set('asf.cache_config_expire', 300); /* 设置缓存多少秒之后过期, 300 seconds by default */
    

    采用框架入口方式

    define('APP_PATH', dirname(__DIR__));
    
    /* 缓存 config.ini 文件 */
    $app = new Asf\Application(APP_PATH . '/config/config.ini');
    $app->run();
    

    使用类 Asf\Config\Php

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

    使用类 Asf\Config\Ini

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

    性能测试

    在开启 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)
    

    传统 MVC, 无内存缓存技术

    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 进程的哟, 不会有共享内存锁的问题。

    Asf 中文帮助文档

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3036 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 12:04 · PVG 20:04 · LAX 05:04 · JFK 08:04
    ♥ Do have faith in what you're doing.