传统的mvc框架
每次请求都会去重新加载配置文件。即使配置文件内容没有更新, 也会去重新加载一次。这是一个很不好的设计。(开启opcache情况下, 还是有执行的过程时间)
asf框架
读取到配置文件的内容保存到系统内存, 下一次请求直接去内存读取数据。asf 也提供非常简单的配置实现 config cache。
什么场景下开启config cache合适?
● 建议在web应用场景下都开启吧, 后面版本可能会默认启用
● 在cli、多线程模式下开启同样生效, 只是php脚本每次执行完就释放了
● 支持数据类型有: strings, arrays, integers, boolean, doubles, floats, null
流程图
开启缓存方法
<?phpini_set('asf.cache_config_enable', 1); /* 开启配置文件缓存 */ini_set('asf.cache_config_expire', 300); /* 设置缓存多少秒之后过期, 300 seconds by default */
框架入口方式加载php/ini配置文件
<?phpdefine('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');
读取配置内容方法
<?phpprint_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 byteshtml transferred: 14259994 bytesrequests 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 byteshtml transferred: 14230000 bytesrequests 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 进程的哟, 不会有共享内存锁的问题。
推荐:《php教程》
以上就是asf php开发之配置信息常驻系统内存的详细内容。