• 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();删除该内存数据
  • 相关阅读:
    SVN资料库转移-----dump和load
    windows Server 2003修改远程连接限制
    oracle定时任务
    Oacle常用语句
    决策树
    Logistic回归
    Matplotlab绘图基础
    基本术语
    看懂执行并优化
    数据分析方法论
  • 原文地址:https://www.cnblogs.com/dluf/p/2921054.html
Copyright © 2020-2023  润新知