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 ( ) 。