php yaconf扩展

在了解到PHP鸟哥还有这个扩展后,我安装尝试了一下

在这里有介绍 https://pecl.php.net/package/yaconf  这里有更详细的代码和说明 https://github.com/laruence/yaconf

好了,到这里git下载代码

 git clone https://github.com/laruence/yaconf 

然后  cd yaconf 

执行这几步操作

phpize7.4
./configure
make
sudo make install

然后发现编译成功了,就去添加一下 php.ini 文件

extension=yaconf

[yaconf]
yaconf.directory=/myyaconf/conf/
yaconf.check_delay=360

添加扩展开启,并配置这两个参数,前面的是要读取的配置文件地址, 后面是多久刷新一次。(但我测试发现这个时间好像没起作用 也许是我测试的方式不太对)

好了,在根目录创建文件夹 

/myyaconf/conf/

给一下需要的权限,然后创建 bar.ini 配置文件文件里面这样写

[base]
parent="yaconf"
children="NULL"


[children:base]               ;inherit from section "base"
children="set"
children2="set2"

权限给这个

-r--r--r-- 1 root root 131 8月  27 18:03 bar.ini

/myyaconf/conf/文件夹的权限给755就行

drwxr-xr-x   3 root root 4.0K 8月  27 17:44 myyaconf/

drwxr-xr-x 2 root root 4.0K 8月  27 17:44 conf/

重启apache2然后看一下效果

<?php
$list = Yaconf::get("bar");
var_dump($list);
exit;

打印结果

array(2) { ["base"]=> array(2) { ["parent"]=> string(6) "yaconf" ["children"]=> string(4) "NULL" } ["children"]=> array(3) { ["parent"]=> string(6) "yaconf" ["children"]=> string(3) "set" ["children2"]=> string(4) "set2" } }

如果是Cli模式执行,则更新ini配置文件后,数据直接就立即加载了,没有等待时间

web模式下没有立即加载,我本地测试的是360秒到期也没重新加载,估计还有其他方面的问题我没考虑到,然后apache2 重启就能加载了。

测试了一下这个速度还是不错的,单次读取的时候花费 0.007ms - 0.01ms.

<?php
for ($i=0; $i < 1; $i++) { 
    $list = Yaconf::get("bar");
}

当我执行10000次的时候,花费 2.02ms - 3.17ms,约每次 0.000202ms - 0.000317ms

<?php
for ($i=0; $i < 10000; $i++) { 
    $list = Yaconf::get("bar");
}

我估计是因为这个获取会设置静态成员变量,首次读取花费时间久,后面就非常快了。

这个配置适合用来加载固定的配置变量常量等,因为它会在php的生命周期里面存活,避免了传统开发中每次都要Include自己需要的配置变量,加快了运行速度

原文地址:https://www.cnblogs.com/lizhaoyao/p/15195451.html