How To Use Shared Memory in PHP如何使用共享内存在PHP
PHP supports shared memory which can be used to store and retrieve data across processes. PHP支持的共享内存可以用来存储和检索数据的过程。 This is also another alternative way to这也是另一种选择方式 communicate between php scripts沟通之间的PHP脚本 . 。 Normally shared memory is used for caching frequently used data in memory for php scripts on the same server.通常共享存储器是用来缓存经常使用的数据在内存中为PHP脚本在同一台服务器上。 Let’s see how we can use shared memory with a simple example.让我们看看我们如何可以使用共用记忆体与一个简单的例子。
How to create PHP shared memory and save a variable (array)如何创建PHP的共用记忆体及储存一个变量(阵列)
Here is a sample code with comments:这里是一个范例程式码与评论:
$key = 'mykey'; // Key to store data with //Returns System V IPC key; 'My test' should be replaced by the pathname of an existing file // as per manual.美元的关键= ' mykey ' ; / /关键数据存储与/ /返回System V的IPC的关键; '我的测试'应改为由路径一个现有的文件/ /按手册。 I found that even a non-existent file works fine.我发现,即使是不存在的档案工作的罚款。 // The second argument is project identifier; a single character of your choice $shm_key = ftok('My test','P'); $data = shm_attach($shm_key); // Pointer to shared memory // Sample data to store $test = array("hello","angsuman","chakraborty"); shm_put_var($data,$inmem,$test); // Save the data in shared memory print_r(shm_get_var($data,$mykey)); // Print the saved data shm_detach($data); // Disconnects from shared memory segment; the data remains intact / /第二个论点是项目标识符;单个字符您所选择的元shm_key = ftok ( '我的测试' ,以字母P ' ) ;元数据= shm_attach ( $ shm_key ) ; / /指针共享内存/ /样本数据,以存储元,试验=阵列( “你好” , “由Angsuman ” , “查敏” ) ; shm_put_var (元数据,元inmem元,测试) ; / /数据保存在共享内存print_r ( shm_get_var (元数据,元mykey ) ) ; / /打印保存的数据shm_detach (元数据) ; / /断开共享内存块;数据保持不变
How to fetch data from shared memory in PHP如何撷取的数据共享内存在PHP
$key = 'mykey'; $shm_key = ftok('My test','P'); $data = shm_attach($shm_key); print_r(shm_get_var($data,$mykey)); shm_detach($data);美元的关键= ' mykey ' ;元shm_key = ftok ( '我的测试' ,以字母P ' ) ;元数据= shm_attach ( $ shm_key ) ; print_r ( shm_get_var (元数据,元mykey ) ) ; shm_detach (元数据) ; Notes:注释:
1. 1 。 The code has been tested on Linux only.代码已经过测试,在Linux上只。
2. 2 。 The arguments to ftok must be same to access the same shared memory from multiple scripts.论据,以ftok必须同时访问同一共享内存从多个脚本。 For use in multiple processes within the same script file use __FILE__ as the first argument to ftok().使用在多个进程在同一脚本文件使用__file__作为第一个论点,以ftok ( ) 。
Filed under提起下 Enterprise Software企业软件 , , Headline News头条新闻 , , How To如何 , , Linux Linux的 , , Open Source Software开放源码软件 , , PHP PHP的 , , Programming编程 , , Web网页 | |
| |
RSS 2.0 2.0 | |
Trackback Trackback跟踪 this Article |此文章|
Email this Article电子邮件此文章
You may also like to read您也可以想读 |




November 13th, 2007 at 12:05 pm 2007年11月13日在下午12时05分
This was an interesting tutorial, nicely done I will have a go at trying this for my self.这是一个有趣的补习,好做,我会有一个去尝试,这为我的自我。
Regards,关心,
Lamonte Harris lamonte哈里斯
November 13th, 2007 at 2:53 pm 2007年11月13日在下午2时53分
It’s fantastic to see a PHP programmer who pays close attention to how much memory they’re using.它的神奇地看到,一个PHP程序员谁自付密切注意多少记忆体,他们正在使用。
February 20th, 2008 at 3:26 am 2008年2月20日在上午03时26分
this is wonderful code and i will test this under my system这是美好的代码和我将测试这下我的系统
thanks谢谢
June 14th, 2008 at 11:48 am 2008年6月14日在上午11时48分
where is the code?哪里是代码? I tried opening the page in both Firefox and IE, and code examples seem to be missing ?我试图打开该网页在Firefox和即和代码的例子似乎是失踪呢?
June 14th, 2008 at 12:16 pm 2008年6月14日在下午12时16分
@Adam @亚当
Thanks.谢谢。 I consider myself more as a Java developer我认为自己更作为一个Java开发
June 14th, 2008 at 12:21 pm 2008年6月14日在下午12时21分
WordPress 2.5.1 screwed up my code.在WordPress 2.5.1拧了我的代码。 It is back again.这是再次回。 Please refresh the page.请刷新页面。