simple implementation with persistence using tmpfs on Linux system
<?php
define('MYSHM_PATH', "/dev/shm/.php_myshm");
@mkdir(MYSHM_PATH);
function shm_set($key, $val, $ttl = 0) {
$key = md5($key);
if ( $ttl > 0 ) $ttl = time() + $ttl;
return file_put_contents(MYSHM_PATH . "/$key" , serialize([$ttl, $val]) , LOCK_EX);
}
function shm_get($key) {
$key = md5($key);
$f = MYSHM_PATH . "/$key";
$v = unserialize(@file_get_contents($f));
if ( ! $v || ($v[0] > 0 && time() > $v[0]) ) {
@unlink($f);
return null;
}
return $v[1];
}
shm_set('teste', ['a', 222 ,453] , 60);
print_r( shm_get('teste') );
?>