一、php生成xml文件的封装类文件-可生成带缩进格式化的xml文件
最近因为有需要,对生成xml文件进行了一些处理,很早之前使用过SimpleXML加载接口返回的xml数据并处理,这次的使用偏向于XML文件的生成。有一个需求,生成的xml文件格式需要格式化好,xml文件在浏览器里看到的样子,一行一行内容,并且不同级别间有内容的缩进控制。这是浏览器自身的功能,实际如果不处理,生成的XML文件字符串间是连续的。例如:
<?xml version="1.0" encoding="utf-8"?>
<student><item><name>liming</name><age>21</age><sex>man</sex><note>喜欢看书</note></item><item><name>lucy</name><age>19</age><sex>woman</sex><note>喜欢看电影</note></item><item><name>lily</name><age>19</age><sex>woman</sex><note>没什么喜欢的</note></item></student>
这样的格式在linux服务器以及在编辑器中查看都特别不好看。于是找实现这种格式化缩进的处理方法。我使用的是DomDocument来生成XML文件,DomDocument是PHP默认支持的类扩展,其中创建节点使用createElement方法,创建文本内容使用createTextNode方法,添加子节点使用appendChild方法,创建属性使用createAttribute方法。其有一项很重要的属性:
formatOutput 布尔类型. Nicely formats output with indentation and extra space.
在创建DomDocument类的时候设置这个属性为true,则在生成的xml文件会自动采用缩进格式,非常方便。封装的类文件及试验类的下载地址 php生成缩进格式的xml文件的封装类及测试 见本博客中的资源搜索。类代码文件Xmlmake.php代码如下:
/*
* Note:生成XML处理类
* Author:linge
* Date:2017-10-23
*/
class Xmlmake
{
//静态例属性.
public static $document = null;
//实例化类
public function __construct($version='1.0', $charset='utf-8')
{
//初始化DomDocument类.并单例化
if(!self::$document)
{
self::$document = new DomDocument($version, $charset);
self::$document->formatOutput = true;
self::$document->preserveWhiteSpace = false;
}
}
//创建元素
public function createElement($element)
{
return self::$document->createElement($element);
}
//添加子节点
public function appendchild($child)
{
return self::$document->appendchild($child);
}
//返回xml字符串
public function saveXML()
{
return self::$document->saveXML();
}
//添加Item
public function createItem($item, $data, $attribute=array())
{
if(is_array($data))
{
foreach ($data as $key => $val)
{
//创建元素,元素名称不能以数字开头.
is_numeric($key{0}) && exit($key. ' error:first char cannot be a number.');
//添加元素
$temp = self::$document->createElement($key);
$item->appendchild($temp);
//添加元素值
$text = self::$document->createTextNode($val);
$temp->appendchild($text);
if(isset($attribute[$key]))
{
//如果此字段存在相关属性需要设置
foreach($attribute[$key] as $akey => $row)
{
//创建属性节点
$temps = self::$document->createAttribute($akey);
$temp->appendchild($temps);
//创建属性值节点
$aval = self::$document->createTextNode($row);
$temps->appendChild($aval);
}
}
}
}
}
//加载Xml文件
public function loadFile($fpath)
{
if(!is_file($fpath))
{
exit($fpath .' is a invalid file.');
}
//成功时返回 TRUE, 或者在失败时返回 FALSE
return self::$document->load($fpath);
}
//保存XML至某人文件.
public function saveFile($fpath)
{
//执行文件写入
return file_put_contents($fpath, self::$document->saveXML());
}
}
测试调用的PHP程序如下:
require 'Xmlmake.php';
$Xmlmake = new Xmlmake();
$data = array(
'liming'=>array('name'=>'liming','age'=>21,'sex'=>'man','note'=>'喜欢看书'),
'lucy'=>array('name'=>'lucy','age'=>19,'sex'=>'woman','note'=>'喜欢看电影'),
'lily'=>array('name'=>'lily','age'=>19,'sex'=>'woman','note'=>'没什么喜欢的'),
);
$Xml = $Xmlmake->createElement('student');
$Xmlmake->appendchild($Xml);
foreach ($data as $student)
{
$item = $Xmlmake->createElement('item');
$Xml->appendchild($item);
$Xmlmake->createItem($item, $student);
}
$Xmlmake->saveFile('student.xml');
echo $Xmlmake->saveXML();
//加载xml内容.
$loadRs = $Xmlmake->loadFile('student.xml');
执行后将生成格式整洁的XML文件,student.xml。格式截图如下:
二、关于opcache缓存的操作小工具cachetool的使用
publish:October 11, 2017 -Wednesday opcache是个提升php性能的利器,但是在线上服务器真实遇到过偶尔几台服务器代码上线后,一直没有生效,查看opcache的配置也没有问题。后来没有办法,就在上线步骤中增加了重启php-fpm的操作。今天发现了一个小工具cachetool。可以方便的使用命令行清除opcache的缓存。
当然除了重启php-fpm的进程可以清理opcache缓存外,opcache本身是支持清除缓存的。手动清理缓存涉及到的opcache函数主要为:opcache_reset()和opcache_invalidate() 。
但是opcache_reset()是php中的函数,需要在php脚本中执行,另外当PHP以PHP-FPM的方式运行的时候,opcache的缓存是无法通过php命令进行清除的,只能通过http或cgi到php-fpm进程的方式来清除缓存。
而opcache_invalidate 废除指定脚本缓存是使指定脚本的字节码缓存失效。可用于明确更新的代码文件列表时使用,但不方便清除整个脚本的缓存。cachetool使用起来也非常方便。如下:
#不需要安装下载下来即可使用
sudo mkdir /opt/modules/cachetool
cd /opt/modules/cachetool
#下载文件并增加可执行权限
sudo curl -sO http://gordalina.github.io/cachetool/downloads/cachetool.phar
sudo chmod +x cachetool.phar
#查看当前的opcache配置
[onlinedev@BFG-OSER-4471 ~]$ php /opt/modules/cachetool/cachetool.phar opcache:configuration --fcgi=127.0.0.1:9000
Zend OPcache 7.0.10
+---------------------------------------+----------------------+
| Directive | Value |
+---------------------------------------+----------------------+
| opcache.enable | true |
| opcache.enable_cli | false |
| opcache.use_cwd | true |
| opcache.validate_timestamps | true |
| opcache.inherited_hack | true |
| opcache.dups_fix | false |
| opcache.revalidate_path | false |
| opcache.log_verbosity_level | 1 |
| opcache.memory_consumption | 1073741824 |
| opcache.interned_strings_buffer | 30 |
| opcache.max_accelerated_files | 4000 |
| opcache.max_wasted_percentage | 0.050000000000000003 |
| opcache.consistency_checks | 0 |
| opcache.force_restart_timeout | 180 |
| opcache.revalidate_freq | 180 |
| opcache.preferred_memory_model | '' |
| opcache.blacklist_filename | '' |
| opcache.max_file_size | 0 |
| opcache.error_log | '' |
| opcache.protect_memory | false |
| opcache.save_comments | false |
| opcache.fast_shutdown | true |
| opcache.enable_file_override | false |
| opcache.optimization_level | 2147467263 |
| opcache.lockfile_path | '/tmp' |
| opcache.file_cache | '' |
| opcache.file_cache_only | false |
| opcache.file_cache_consistency_checks | true |
+---------------------------------------+----------------------+
#列表查看opcache缓存的脚本
[onlinedev@BFG-OSER-4471 ~]$ php /opt/modules/cachetool/cachetool.phar opcache:status:scripts --fcgi=127.0.0.1:9000
+------+-----------+---------------------------------------------------------------------------+
| Hits | Memory | Filename |
+------+-----------+---------------------------------------------------------------------------+
| 8 | 4.16 KiB | /opt/data/api/revs/r201710111538_1367/vendor/composer/autoload_static.php |
| 8 | 5.95 KiB | /opt/data/api/revs/r201710111538_1367/www/index.php |
| 8 | 23.85 KiB | /opt/data/api/revs/r201710111538_1367/conf/Config.php |
| 8 | 848 b | /opt/data/api/revs/r201710111538_1367/vendor/autoload.php |
| 8 | 84.88 KiB | /opt/data/api/revs/r201710111538_1367/lib/util/Params.php |
| 8 | 30.48 KiB | /opt/data/api/revs/r201710111538_1367/lib/Log.php |
| 8 | 6.34 KiB | /opt/data/api/revs/r201710111538_1367/vendor/composer/autoload_real.php |
| 8 | 51.69 KiB | /opt/data/api/revs/r201710111538_1367/app/controller/DebugController.php |
| 8 | 6.48 KiB | /opt/data/api/revs/r201710111538_1367/lib/Controller.php |
| 8 | 23.84 KiB | /opt/data/api/revs/r201710111538_1367/vendor/composer/ClassLoader.php |
+------+-----------+---------------------------------------------------------------------------+
#查看当前的opcache缓存的统计信息
[onlinedev@BFG-OSER-4471 ~]$ php /opt/modules/cachetool/cachetool.phar opcache:status --fcgi=127.0.0.1:9000
+----------------------+---------------------------------+
| Name | Value |
+----------------------+---------------------------------+
| Enabled | Yes |
| Cache full | No |
| Restart pending | No |
| Restart in progress | No |
| Memory used | 66.66 MiB |
| Memory free | 957.34 MiB |
| Memory wasted (%) | 0 b (0%) |
| Strings buffer size | 30 MiB |
| Strings memory used | 221.27 KiB |
| Strings memory free | 29.78 MiB |
| Number of strings | 5501 |
+----------------------+---------------------------------+
| Cached scripts | 10 |
| Cached keys | 16 |
| Max cached keys | 7963 |
| Start time | Wed, 11 Oct 2017 03:30:16 +0000 |
| Last restart time | Wed, 11 Oct 2017 07:38:20 +0000 |
| Oom restarts | 0 |
| Hash restarts | 0 |
| Manual restarts | 4 |
| Hits | 100 |
| Misses | 22 |
| Blacklist misses (%) | 0 (0%) |
| Opcache hit rate | 81.967213114754 |
+----------------------+---------------------------------+
#执行清理opcache缓存
[onlinedev@BFG-OSER-4471 ~]$ php /opt/modules/cachetool/cachetool.phar opcache:reset --fcgi=127.0.0.1:9000
#再次查看opcache缓存信息,会发现Cached scripts已被清空。
[onlinedev@BFG-OSER-4471 ~]$ php /opt/modules/cachetool/cachetool.phar opcache:status --fcgi=127.0.0.1:9000
+----------------------+---------------------------------+
| Name | Value |
+----------------------+---------------------------------+
| Enabled | Yes |
| Cache full | No |
| Restart pending | No |
| Restart in progress | No |
| Memory used | 66.43 MiB |
| Memory free | 957.57 MiB |
| Memory wasted (%) | 0 b (0%) |
| Strings buffer size | 30 MiB |
| Strings memory used | 171.92 KiB |
| Strings memory free | 29.83 MiB |
| Number of strings | 4283 |
+----------------------+---------------------------------+
| Cached scripts | 0 |
| Cached keys | 0 |
| Max cached keys | 7963 |
| Start time | Wed, 11 Oct 2017 03:30:16 +0000 |
| Last restart time | Wed, 11 Oct 2017 08:36:17 +0000 |
| Oom restarts | 0 |
| Hash restarts | 0 |
| Manual restarts | 5 |
| Hits | 0 |
| Misses | 2 |
| Blacklist misses (%) | 0 (0%) |
| Opcache hit rate | 0 |
+----------------------+---------------------------------+
cachetool除了可操作opcache缓存外,还可以操作apc缓存。所有的方法列表如下.
官方文档地址:http://gordalina.github.io/cachetool/
apc
apc:bin:dump Get a binary dump of files and user variables
apc:bin:load Load a binary dump into the APC file and user variables
apc:cache:clear Clears APC cache (user, system or all)
apc:cache:info Shows APC user & system cache information
apc:cache:info:file Shows APC file cache information
apc:key:delete Deletes an APC key
apc:key:exists Checks if an APC key exists
apc:key:fetch Shows the content of an APC key
apc:key:store Store an APC key with given value
apc:sma:info Show APC shared memory allocation information
opcache
opcache:configuration Get configuration information about the cache
opcache:reset Resets the contents of the opcode cache
opcache:status Show summary information about the opcode cache
opcache:status:scripts Show scripts in the opcode cache