shmop php在linux上操作共享内存的函数
关于php操作内存,我选择了shmop扩展,其方法只有6个:
- shmop_open 打开内存块.
- shmop_write 向内存块中写入数据
- shmop_size 获得内存块大小
- shmop_read 读取内存块数据
- shmop_delete 删除内存块数据
- shmop_close 关闭内存块
<?php class shared { private $shm_id; private $shm_key = 0xff3; private $shm_size = 1024; function __construct() { $this->shm_id = shmop_open($this->shm_key, "c", 0644, $this->shm_size) or die('申请失败'); } function __get($name) { $buf = shmop_read($this->shm_id, 0, $this->shm_size); $buf = unserialize(trim($buf)); if ($name == '_all') return $buf; return isset($buf[$name]) ? $buf[$name] : false; } function __set($name, $value) { $buf = shmop_read($this->shm_id, 0, $this->shm_size); $buf = unserialize(trim($buf)); $buf[$name] = $value; $buf = serialize($buf); if (strlen($buf) >= $this->shm_size) die('空间不足'); shmop_write($this->shm_id, $buf, 0) or die('写入失败'); } function del() { shmop_delete($this->shm_id); } } $shmopobj = new shared(); if (isset($shmopobj->database) || $shmopobj->database == null) { echo "input memory"; echo "</br>"; $shmopobj->database = "localhost"; } else { echo "get data"; echo "</br>"; echo $shmopobj->database; } //$shmopobj->del(); ?>
第一次刷新页面,输出
input memory
第二次刷新页面,输出
get data
localhost
然后可以调用
$shmopobj->del();删除该内存数据