bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

php操作共享內(nèi)存shmop類(lèi)如何使用

這篇文章將為大家詳細(xì)講解有關(guān)php操作共享內(nèi)存shmop類(lèi)如何使用,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)博野免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

SimpleSHM 是一個(gè)較小的抽象層,用于使用 PHP 操作共享內(nèi)存,支持以一種面向?qū)ο蟮姆绞捷p松操作內(nèi)存段。在編寫(xiě)使用共享內(nèi)存進(jìn)行存儲(chǔ)的小型應(yīng)用程序時(shí),這個(gè)庫(kù)可幫助創(chuàng)建非常簡(jiǎn)潔的代碼。可以使用 3 個(gè)方法進(jìn)行處理:讀、寫(xiě)和刪除。從該類(lèi)中簡(jiǎn)單地實(shí)例化一個(gè)對(duì)象,可以控制打開(kāi)的共享內(nèi)存段。

類(lèi)對(duì)象和測(cè)試代碼

<?php
//類(lèi)對(duì)象
namespace Simple\SHM;
class Block
{
    /**
     * Holds the system id for the shared memory block
     *
     * @var int
     * @access protected
     */
    protected $id;
    /**
     * Holds the shared memory block id returned by shmop_open
     *
     * @var int
     * @access protected
     */
    protected $shmid;
    /**
     * Holds the default permission (octal) that will be used in created memory blocks
     *
     * @var int
     * @access protected
     */
    protected $perms = 0644;
    /**
     * Shared memory block instantiation
     *
     * In the constructor we'll check if the block we're going to manipulate
     * already exists or needs to be created. If it exists, let's open it.
     *
     * @access public
     * @param string $id (optional) ID of the shared memory block you want to manipulate
     */
    public function __construct($id = null)
    {
        if($id === null) {
            $this->id = $this->generateID();
        } else {
            $this->id = $id;
        }
        if($this->exists($this->id)) {
            $this->shmid = shmop_open($this->id, "w", 0, 0);
        }
    }
    /**
     * Generates a random ID for a shared memory block
     *
     * @access protected
     * @return int System V IPC key generated from pathname and a project identifier
     */
    protected function generateID()
    {
        $id = ftok(__FILE__, "b");
        return $id;
    }
    /**
     * Checks if a shared memory block with the provided id exists or not
     *
     * In order to check for shared memory existance, we have to open it with
     * reading access. If it doesn't exist, warnings will be cast, therefore we
     * suppress those with the @ operator.
     *
     * @access public
     * @param string $id ID of the shared memory block you want to check
     * @return boolean True if the block exists, false if it doesn't
     */
    public function exists($id)
    {
        $status = @shmop_open($id, "a", 0, 0);
        return $status;
    }
    /**
     * Writes on a shared memory block
     *
     * First we check for the block existance, and if it doesn't, we'll create it. Now, if the
     * block already exists, we need to delete it and create it again with a new byte allocation that
     * matches the size of the data that we want to write there. We mark for deletion,  close the semaphore
     * and create it again.
     *
     * @access public
     * @param string $data The data that you wan't to write into the shared memory block
     */
    public function write($data)
    {
        $size = mb_strlen($data, 'UTF-8');
        if($this->exists($this->id)) {
            shmop_delete($this->shmid);
            shmop_close($this->shmid);
            $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
            shmop_write($this->shmid, $data, 0);
        } else {
            $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
            shmop_write($this->shmid, $data, 0);
        }
    }
    /**
     * Reads from a shared memory block
     *
     * @access public
     * @return string The data read from the shared memory block
     */
    public function read()
    {
        $size = shmop_size($this->shmid);
        $data = shmop_read($this->shmid, 0, $size);
        return $data;
    }
    /**
     * Mark a shared memory block for deletion
     *
     * @access public
     */
    public function delete()
    {
        shmop_delete($this->shmid);
    }
    /**
     * Gets the current shared memory block id
     *
     * @access public
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Gets the current shared memory block permissions
     *
     * @access public
     */
    public function getPermissions()
    {
        return $this->perms;
    }
    /**
     * Sets the default permission (octal) that will be used in created memory blocks
     *
     * @access public
     * @param string $perms Permissions, in octal form
     */
    public function setPermissions($perms)
    {
        $this->perms = $perms;
    }
    /**
     * Closes the shared memory block and stops manipulation
     *
     * @access public
     */
    public function __destruct()
    {
        shmop_close($this->shmid);
    }
}
<?php
//測(cè)試使用代碼
namespace Simple\SHM\Test;
use Simple\SHM\Block;
class BlockTest extends \PHPUnit_Framework_TestCase
{
    public function testIsCreatingNewBlock()
    {
        $memory = new Block;
        $this->assertInstanceOf('Simple\\SHM\\Block', $memory);
        $memory->write('Sample');
        $data = $memory->read();
        $this->assertEquals('Sample', $data);
    }
    public function testIsCreatingNewBlockWithId()
    {
        $memory = new Block(897);
        $this->assertInstanceOf('Simple\\SHM\\Block', $memory);
        $this->assertEquals(897, $memory->getId());
        $memory->write('Sample 2');
        $data = $memory->read();
        $this->assertEquals('Sample 2', $data);
    }
    public function testIsMarkingBlockForDeletion()
    {
        $memory = new Block(897);
        $memory->delete();
        $data = $memory->read();
        $this->assertEquals('Sample 2', $data);
    }
    public function testIsPersistingNewBlockWithoutId()
    {
        $memory = new Block;
        $this->assertInstanceOf('Simple\\SHM\\Block', $memory);
        $memory->write('Sample 3');
        unset($memory);
        $memory = new Block;
        $data = $memory->read();
        $this->assertEquals('Sample 3', $data);
    }
}

額外說(shuō)明

<?php
 
$memory = new SimpleSHM;
$memory->write('Sample');
echo $memory->read();
 
?>

請(qǐng)注意,上面代碼里沒(méi)有為該類(lèi)傳遞一個(gè) ID。如果沒(méi)有傳遞 ID,它將隨機(jī)選擇一個(gè)編號(hào)并打開(kāi)該編號(hào)的新內(nèi)存段。我們可以以參數(shù)的形式傳遞一個(gè)編號(hào),供構(gòu)造函數(shù)打開(kāi)現(xiàn)有的內(nèi)存段,或者創(chuàng)建一個(gè)具有特定 ID 的內(nèi)存段,如下

<?php
 
$new = new SimpleSHM(897);
$new->write('Sample');
echo $new->read();
 
?>

神奇的方法 __destructor 負(fù)責(zé)在該內(nèi)存段上調(diào)用 shmop_close 來(lái)取消設(shè)置對(duì)象,以與該內(nèi)存段分離。我們將這稱(chēng)為 “SimpleSHM 101”。現(xiàn)在讓我們將此方法用于更高級(jí)的用途:使用共享內(nèi)存作為存儲(chǔ)。存儲(chǔ)數(shù)據(jù)集需要序列化,因?yàn)閿?shù)組或?qū)ο鬅o(wú)法存儲(chǔ)在內(nèi)存中。盡管這里使用了 JSON 來(lái)序列化,但任何其他方法(比如 XML 或內(nèi)置的 PHP 序列化功能)也已足夠。如下

<?php
 
require('SimpleSHM.class.php');
 
$results = array(
    'user' => 'John',
    'password' => '123456',
    'posts' => array('My name is John', 'My name is not John')
);
 
$data = json_encode($results);
 
$memory = new SimpleSHM;
$memory->write($data);
$storedarray = json_decode($memory->read());
 
print_r($storedarray);
 
?>

我們成功地將一個(gè)數(shù)組序列化為一個(gè) JSON 字符串,將它存儲(chǔ)在共享內(nèi)存塊中,從中讀取數(shù)據(jù),去序列化 JSON 字符串,并顯示存儲(chǔ)的數(shù)組。這看起來(lái)很簡(jiǎn)單,但請(qǐng)想象一下這個(gè)代碼片段帶來(lái)的可能性。您可以使用它存儲(chǔ) Web 服務(wù)請(qǐng)求、數(shù)據(jù)庫(kù)查詢(xún)或者甚至模板引擎緩存的結(jié)果。在內(nèi)存中讀取和寫(xiě)入將帶來(lái)比在磁盤(pán)中讀取和寫(xiě)入更高的性能。

使用此存儲(chǔ)技術(shù)不僅對(duì)緩存有用,也對(duì)應(yīng)用程序之間的數(shù)據(jù)交換也有用,只要數(shù)據(jù)以?xún)啥硕伎勺x的格式存儲(chǔ)。不要低估共享內(nèi)存在 Web 應(yīng)用程序中的力量。可采用許多不同的方式來(lái)巧妙地實(shí)現(xiàn)這種存儲(chǔ),惟一的限制是開(kāi)發(fā)人員的創(chuàng)造力和技能。

關(guān)于php操作共享內(nèi)存shmop類(lèi)如何使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

分享名稱(chēng):php操作共享內(nèi)存shmop類(lèi)如何使用
文章源于:http://vcdvsql.cn/article38/peeppp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序云服務(wù)器建站公司靜態(tài)網(wǎng)站網(wǎng)站維護(hù)網(wǎng)站營(yíng)銷(xiāo)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)