• php shmop 内存读写操作


    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();删除该内存数据
  • 相关阅读:
    宏定义中的常见使用
    VS 对于LINK fatal Error 问题 解决方案
    cocos2d-x中常见的场景切换
    给新建的Cocos2d-x 3.X的Win32工程添加CocoStudio库
    2048之军衔篇 反馈 有事留言
    http 错误代码表
    华为S5700交换机初始化和配置SSH和TELNET远程登录方法
    Linux修改网卡名
    Linux如何配置bond
    linux系统下如何挂载NTFS移动硬盘
  • 原文地址:https://www.cnblogs.com/dluf/p/2921054.html
Copyright © 2020-2023  润新知