php-ipc/examples/shmdata.php

35 lines
923 B
PHP

<?php
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Ipc\Shm\SharedData;
use NoccyLabs\Ipc\Key\FileKey;
$key = new FileKey(__FILE__);
$shm = new SharedData($key);
// Set works as expected
$shm->set("some.key", "Some value");
// As does get
echo $shm->get("some.key")."\n";
// To make sure a value isn't modified while you are modifying it, use the third
// parameter to set...
//
// Let's start at 123
$shm->set("some.counter", 123);
$counter = $shm->get("some.counter");
echo "Counter is: ".$counter."\n";
// And attempt to increase it
$shm->set("some.counter", $counter + 1, true);
$counter = $shm->get("some.counter");
echo "Counter is: ".$counter."\n";
// If the value is modified, the call will fail
$shm2 = new SharedData($key);
$shm2->set("some.counter", 345);
if (!$shm->set("some.counter", $counter + 1, true)) {
echo "some.counter has been modified since last read\n";
}
$shm->destroy();