PHP supports shared memory which can be used to store and retrieve data across processes. PHP supporta memoria condivisa che può essere utilizzata per memorizzare e recuperare i dati tra processi. This is also another alternative way to Questo è anche un altro modo alternativo per communicate between php scripts comunicare tra di script PHP . Normally shared memory is used for caching frequently used data in memory for php scripts on the same server. Normalmente memoria condivisa viene utilizzata per la memorizzazione nella cache frequentemente utilizzati dati in memoria per PHP script sullo stesso server. Let’s see how we can use shared memory with a simple example. Vediamo come si possa utilizzare la memoria condivisa con un semplice esempio.


How to create PHP shared memory and save a variable (array) Come creare PHP memoria condivisa e salvare una variabile (array)

Here is a sample code with comments: Qui c'è un esempio di codice con commenti:

 $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. $ key = 'mykey'; / / chiave per memorizzare i dati con / / Restituisce System V IPC chiave; 'Il mio test' dovrebbe essere sostituito con il percorso di un file esistente / / come da manuale. I found that even a non-existent file works fine. Ho trovato che anche un inesistente file funziona bene. // 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 / / Il secondo argomento è identificatore di progetto; un singolo carattere di tua scelta shm_key $ = ftok ( 'La mia prova', 'P'); $ data = shm_attach ($ shm_key); / / Puntatore alla memoria condivisa / / Esempi di dati a negozio prova $ = array ( "ciao", "angsuman", "Chakraborty"); shm_put_var ($ dati, $ inmem, $ prova); / / Salva i dati in memoria condivisa print_r (shm_get_var ($ dati, $ mykey)) / / Stampa la dati salvati shm_detach ($ dati); / / disconnette dal segmento di memoria condivisa; dati rimane intatto 

How to fetch data from shared memory in PHP Come recuperare i dati di memoria condivisa in PHP

 $key = 'mykey'; $shm_key = ftok('My test','P'); $data =  shm_attach($shm_key); print_r(shm_get_var($data,$mykey)); shm_detach($data); $ key = 'mykey'; shm_key $ = ftok ( 'La mia prova', 'P'); $ data = shm_attach ($ shm_key); print_r (shm_get_var ($ dati, $ mykey)); shm_detach ($ dati); 

Notes: Note:
1. The code has been tested on Linux only. Il codice è stato testato solo su Linux.
2. The arguments to ftok must be same to access the same shared memory from multiple scripts. Le argomentazioni a ftok deve essere lo stesso per accedere alla stessa memoria condivisa da più script. For use in multiple processes within the same script file use __FILE__ as the first argument to ftok(). Per l'uso in più processi all'interno dello stesso file di script utilizzare __FILE__ come primo argomento a ftok ().