This package is wrapper for php sysv shm_xxx.
from Packagist
composer require takuya/php-sysv-ipc-shared-memory
from GitHub
name='php-sysv-ipc-shared-memory'
composer config repositories.$name \
vcs https://github.com/takuya/$name
composer require takuya/$name:master
composer install
<?php
$uniq_name = 'shm_name';
$shm = new IPCSharedMem($uniq_name);
$shm->put(new MyClass());
//
$obj = $shm->get();// instance of MyClass;
// remove ipc
$shm->destroy()
call get() then set()
in the lock to update value using semaphore.
<?php
$idx = 'key';
$store = new IPCShmKeyStore('shm_name');
$store->runWithLock(function($store)use($idx){
$store->set($store,$shm->get($idx)+1);
});
This package offers KVS style access to Shared Memory.
<?php
$store = new IPCShmKeyStore('kvs-like', 1024*1024);
// Set by key
$store->set('key',['msg'=>'Auxakai3']);
// Get by key
$store->get('key')['msg']; // => Auxakai3
This package offers ArrayAccess style to use Shared Memory.
<?php
$arr = new IPCShmKeyStore('array-like', 100);
$arr[] = 'a';
$arr[] = 'b';
$arr[] = 'c';
foreach($arr as $e){
echo "$e,";
}
// => "a,b,c,"
Limitation: ArrayAccess is not a real 'array'. Array functions ( ex array_map()
) cannot be applied for this.
Compare to shared memory functions ( ex shmop_open()
) , One big advantage SysV functions has.
Sysv function (ex shm_put_var
) has auto serialization.
I wrote these php code.
- SysV IPC Wrapper
- shm_open
- psr/simplecache
- maintenance
- laravel cache
If unused ipc remains. use SHELL command to remove.
ipcs -m | grep $USER | grep -oE '0x[a-f0-9]+' | xargs -I@ ipcrm --shmem-key @