Skip to content

Instantly share code, notes, and snippets.

@johnlettman-old
Created August 16, 2011 19:20
Show Gist options
  • Save johnlettman-old/1149910 to your computer and use it in GitHub Desktop.
Save johnlettman-old/1149910 to your computer and use it in GitHub Desktop.
Persistent shared memory using PHP -- perfect for loading a webapplication's configuration one time only and quickly deserializing it per request. Faster than disk configuration mediums, including PHP-file configs.
<?php
// Open the memory location; read-only.
$memoryHandle = shmop_open(0x13c, "a", 0644, 100);
// Read the memory location and print it.
$string = shmop_read($memoryHandle, 0, 0); # Setting the byte count to 0 reads all bytes.
echo 'Data in RAM reads: '.$string;
shmop_close($memoryHandle);
<?php
// Open the memory location; create/write.
$memoryHandle = shmop_open(0x13c, "c", 0644, 100);
// Set the data from the GET variable.
if(isset($_GET['data'])) {
shmop_write($memoryHandle, $_GET['data'], 0);
echo 'Wrote: '.$_GET['data'];
}
shmop_close($memoryHandle);
@chinaestone
Copy link

writer.php should close $memoryHandle, not $shm_id .

Here is the right code .

<?php
// Open the memory location; create/write.
$memoryHandle = shmop_open(0x13c, "c", 0644, 100);
// Set the data from the GET variable.
if(isset($_GET['data'])) {
	shmop_write($memoryHandle, $_GET['data'], 0);
	echo 'Wrote: '.$_GET['data'];
}
shmop_close($memoryHandle);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment