2018-04-15 15:25:55 +00:00
|
|
|
<?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";
|
|
|
|
}
|
|
|
|
|
2018-04-15 16:50:19 +00:00
|
|
|
$shm->destroy();
|