PHP中的共享内存文件 [英] Shared memory file in PHP
问题描述
我使用openssl_pkcs7_sign
和openssl_pkcs7_encrypt
创建加密数据.这些功能仅接受文件名.我想将临时文件存储在共享内存中以提高性能.我了解在Linux中我可以file_put_contents('/dev/shm/xxx', data)
,但是对于Windows是不可能的. PHP中是否有可移植的方式来做到这一点? shmop_
函数在这里有帮助吗?谢谢.
I use openssl_pkcs7_sign
and openssl_pkcs7_encrypt
to create encrypted data. The functions only accept file names. I would like to store the temporary files in shared memory to improve performance. I understand in Linux I can file_put_contents('/dev/shm/xxx', data)
, but it is not possible for Windows. Is there portable way in PHP to do this? Would shmop_
function help here? Thanks.
PS:还是有办法使这些函数接受数据字符串?
PS: Or is there way to make these functions accept data strings?
PS2:请不要建议从PHP调用/usr/bin/openssl
.它不是便携式的.
PS2: Please do not suggest invoking /usr/bin/openssl
from PHP. It is not portable.
推荐答案
从Windows 2000开始,shmop
(以前为shm_
)方法可用.
Since Windows 2000, the shmop
(previously shm_
) methods are available.
shmop_open
使用唯一的整数键共享存储区. ftok
可用于根据文件路径(通常是脚本文件的完整路径)生成唯一索引.共享相同密钥的任何实例都可以共享相同的内存.
shmop_open
uses a unique integer key to share the memory area. ftok
can be used to produce a unique index based on a file path (typically your script file's full path). Any instance that shares the same key can share the same memory.
http://php.net/manual/en/ref.shmop.php
在Zend Server CE的PHP版本5.3.3上进行了测试 系统Windows NT CRYPE 6.1内部版本7601(未知Windows版本Business Edition Service Pack 1)i586
Tested on PHP Version 5.3.3 from Zend Server CE System Windows NT CRYPE 6.1 build 7601 (Unknow Windows version Business Edition Service Pack 1) i586
<?php
$key = ftok(__FILE__, 't');
$memory = shmop_open($key, "c", 0600, 16 * 1024);
$data = array('data' => 'value');
$bytes = shmop_write($memory, serialize($data), 0);
$return = shmop_read($memory, 0, $bytes);
print_r(unserialize($return));
?>
shmop_read
/shmop_write
存储来自字符串的原始字节,因此您不需要序列化它,但是您需要将字符串的长度写在某个地方.我的示例创建了一个16KB的共享内存区域,您当然可以调整它的大小以适合openssl文件以及存储文件大小所需的空间.
shmop_read
/shmop_write
stores raw bytes from a string, so you don't need to serialize it, but you will need to write the length of the string somewhere. My example creates a 16KB shared memory area, you can of course size it to fit the openssl file plus the space you need to store the file size.
这篇关于PHP中的共享内存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!