您好,欢迎访问一九零五行业门户网

php操作共享内存shmop类及简单使用测试的代码

这篇文章主要介绍了关于php操作共享内存shmop类及简单使用测试的代码,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
simpleshm 是一个较小的抽象层,用于使用 php 操作共享内存,支持以一种面向对象的方式轻松操作内存段。在编写使用共享内存进行存储的小型应用程序时,这个库可帮助创建非常简洁的代码。可以使用 3 个方法进行处理:读、写和删除。从该类中简单地实例化一个对象,可以控制打开的共享内存段。
类对象和测试代码
<?php//类对象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//测试使用代码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); }}
额外说明
<?php $memory = new simpleshm;$memory->write('sample');echo $memory->read(); ?>
请注意,上面代码里没有为该类传递一个 id。如果没有传递 id,它将随机选择一个编号并打开该编号的新内存段。我们可以以参数的形式传递一个编号,供构造函数打开现有的内存段,或者创建一个具有特定 id 的内存段,如下
<?php $new = new simpleshm(897);$new->write('sample');echo $new->read(); ?>
神奇的方法 __destructor 负责在该内存段上调用 shmop_close 来取消设置对象,以与该内存段分离。我们将这称为 “simpleshm 101”。现在让我们将此方法用于更高级的用途:使用共享内存作为存储。存储数据集需要序列化,因为数组或对象无法存储在内存中。尽管这里使用了 json 来序列化,但任何其他方法(比如 xml 或内置的 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); ?>
我们成功地将一个数组序列化为一个 json 字符串,将它存储在共享内存块中,从中读取数据,去序列化 json 字符串,并显示存储的数组。这看起来很简单,但请想象一下这个代码片段带来的可能性。您可以使用它存储 web 服务请求、数据库查询或者甚至模板引擎缓存的结果。在内存中读取和写入将带来比在磁盘中读取和写入更高的性能。
使用此存储技术不仅对缓存有用,也对应用程序之间的数据交换也有用,只要数据以两端都可读的格式存储。不要低估共享内存在 web 应用程序中的力量。可采用许多不同的方式来巧妙地实现这种存储,惟一的限制是开发人员的创造力和技能。
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
php实现共享内存进程通信函数(_shm)
以上就是php操作共享内存shmop类及简单使用测试的代码的详细内容。
其它类似信息

推荐信息