资讯专栏INFORMATION COLUMN

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

rockswang / 1245人阅读

摘要:是一个较小的抽象层,用于使用操作共享内存,支持以一种面向对象的方式轻松操作内存段。在编写使用共享内存进行存储的小型应用程序时,这个库可帮助创建非常简洁的代码。不要低估共享内存在应用程序中的力量。

SimpleSHM 是一个较小的抽象层,用于使用 PHP 操作共享内存,支持以一种面向对象的方式轻松操作内存段。在编写使用共享内存进行存储的小型应用程序时,这个库可帮助创建非常简洁的代码。可以使用 3 个方法进行处理:读、写和删除。从该类中简单地实例化一个对象,可以控制打开的共享内存段。

类对象和测试代码

</>复制代码

  1. id = $this->generateID();
  2. } else {
  3. $this->id = $id;
  4. }
  5. if($this->exists($this->id)) {
  6. $this->shmid = shmop_open($this->id, "w", 0, 0);
  7. }
  8. }
  9. /**
  10. * Generates a random ID for a shared memory block
  11. *
  12. * @access protected
  13. * @return int System V IPC key generated from pathname and a project identifier
  14. */
  15. protected function generateID()
  16. {
  17. $id = ftok(__FILE__, "b");
  18. return $id;
  19. }
  20. /**
  21. * Checks if a shared memory block with the provided id exists or not
  22. *
  23. * In order to check for shared memory existance, we have to open it with
  24. * reading access. If it doesn"t exist, warnings will be cast, therefore we
  25. * suppress those with the @ operator.
  26. *
  27. * @access public
  28. * @param string $id ID of the shared memory block you want to check
  29. * @return boolean True if the block exists, false if it doesn"t
  30. */
  31. public function exists($id)
  32. {
  33. $status = @shmop_open($id, "a", 0, 0);
  34. return $status;
  35. }
  36. /**
  37. * Writes on a shared memory block
  38. *
  39. * First we check for the block existance, and if it doesn"t, we"ll create it. Now, if the
  40. * block already exists, we need to delete it and create it again with a new byte allocation that
  41. * matches the size of the data that we want to write there. We mark for deletion, close the semaphore
  42. * and create it again.
  43. *
  44. * @access public
  45. * @param string $data The data that you wan"t to write into the shared memory block
  46. */
  47. public function write($data)
  48. {
  49. $size = mb_strlen($data, "UTF-8");
  50. if($this->exists($this->id)) {
  51. shmop_delete($this->shmid);
  52. shmop_close($this->shmid);
  53. $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
  54. shmop_write($this->shmid, $data, 0);
  55. } else {
  56. $this->shmid = shmop_open($this->id, "c", $this->perms, $size);
  57. shmop_write($this->shmid, $data, 0);
  58. }
  59. }
  60. /**
  61. * Reads from a shared memory block
  62. *
  63. * @access public
  64. * @return string The data read from the shared memory block
  65. */
  66. public function read()
  67. {
  68. $size = shmop_size($this->shmid);
  69. $data = shmop_read($this->shmid, 0, $size);
  70. return $data;
  71. }
  72. /**
  73. * Mark a shared memory block for deletion
  74. *
  75. * @access public
  76. */
  77. public function delete()
  78. {
  79. shmop_delete($this->shmid);
  80. }
  81. /**
  82. * Gets the current shared memory block id
  83. *
  84. * @access public
  85. */
  86. public function getId()
  87. {
  88. return $this->id;
  89. }
  90. /**
  91. * Gets the current shared memory block permissions
  92. *
  93. * @access public
  94. */
  95. public function getPermissions()
  96. {
  97. return $this->perms;
  98. }
  99. /**
  100. * Sets the default permission (octal) that will be used in created memory blocks
  101. *
  102. * @access public
  103. * @param string $perms Permissions, in octal form
  104. */
  105. public function setPermissions($perms)
  106. {
  107. $this->perms = $perms;
  108. }
  109. /**
  110. * Closes the shared memory block and stops manipulation
  111. *
  112. * @access public
  113. */
  114. public function __destruct()
  115. {
  116. shmop_close($this->shmid);
  117. }
  118. }

</>复制代码

  1. assertInstanceOf("SimpleSHMBlock", $memory);
  2. $memory->write("Sample");
  3. $data = $memory->read();
  4. $this->assertEquals("Sample", $data);
  5. }
  6. public function testIsCreatingNewBlockWithId()
  7. {
  8. $memory = new Block(897);
  9. $this->assertInstanceOf("SimpleSHMBlock", $memory);
  10. $this->assertEquals(897, $memory->getId());
  11. $memory->write("Sample 2");
  12. $data = $memory->read();
  13. $this->assertEquals("Sample 2", $data);
  14. }
  15. public function testIsMarkingBlockForDeletion()
  16. {
  17. $memory = new Block(897);
  18. $memory->delete();
  19. $data = $memory->read();
  20. $this->assertEquals("Sample 2", $data);
  21. }
  22. public function testIsPersistingNewBlockWithoutId()
  23. {
  24. $memory = new Block;
  25. $this->assertInstanceOf("SimpleSHMBlock", $memory);
  26. $memory->write("Sample 3");
  27. unset($memory);
  28. $memory = new Block;
  29. $data = $memory->read();
  30. $this->assertEquals("Sample 3", $data);
  31. }
  32. }

额外说明

</>复制代码

  1. write("Sample");
  2. echo $memory->read();
  3. ?>

请注意,上面代码里没有为该类传递一个 ID。如果没有传递 ID,它将随机选择一个编号并打开该编号的新内存段。我们可以以参数的形式传递一个编号,供构造函数打开现有的内存段,或者创建一个具有特定 ID 的内存段,如下

</>复制代码

  1. write("Sample");
  2. echo $new->read();
  3. ?>

神奇的方法 __destructor 负责在该内存段上调用 shmop_close 来取消设置对象,以与该内存段分离。我们将这称为 “SimpleSHM 101”。现在让我们将此方法用于更高级的用途:使用共享内存作为存储。存储数据集需要序列化,因为数组或对象无法存储在内存中。尽管这里使用了 JSON 来序列化,但任何其他方法(比如 XML 或内置的 PHP 序列化功能)也已足够。如下

</>复制代码

  1. "John",
  2. "password" => "123456",
  3. "posts" => array("My name is John", "My name is not John")
  4. );
  5. $data = json_encode($results);
  6. $memory = new SimpleSHM;
  7. $memory->write($data);
  8. $storedarray = json_decode($memory->read());
  9. print_r($storedarray);
  10. ?>

我们成功地将一个数组序列化为一个 JSON 字符串,将它存储在共享内存块中,从中读取数据,去序列化 JSON 字符串,并显示存储的数组。这看起来很简单,但请想象一下这个代码片段带来的可能性。您可以使用它存储 Web 服务请求、数据库查询或者甚至模板引擎缓存的结果。在内存中读取和写入将带来比在磁盘中读取和写入更高的性能。

使用此存储技术不仅对缓存有用,也对应用程序之间的数据交换也有用,只要数据以两端都可读的格式存储。不要低估共享内存在 Web 应用程序中的力量。可采用许多不同的方式来巧妙地实现这种存储,惟一的限制是开发人员的创造力和技能。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/61998.html

相关文章

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

    摘要:是一个较小的抽象层,用于使用操作共享内存,支持以一种面向对象的方式轻松操作内存段。在编写使用共享内存进行存储的小型应用程序时,这个库可帮助创建非常简洁的代码。不要低估共享内存在应用程序中的力量。 SimpleSHM 是一个较小的抽象层,用于使用 PHP 操作共享内存,支持以一种面向对象的方式轻松操作内存段。在编写使用共享内存进行存储的小型应用程序时,这个库可帮助创建非常简洁的代码。可以...

    huashiou 评论0 收藏0
  • php简单使用shmop函数创建共享内存减少服务器负载

    摘要:请注意,此函数返回一个编号,其他函数可使用该编号操作该共享内存段。从内存段读取数据从共享内存段读取数据很简单。函数将该内存段标记为删除,阻止任何其他进程打开它。 在之前的一篇博客[了解一下共享内存的概念及优缺点]已经对共享内存的概念做了说明。下面就来简单使用共享内存(其实也可以用其他工具,比如redis) PHP做内存共享有两套接口。一个是shm,它实际上是变量共享,会把对象变量序列化...

    PingCAP 评论0 收藏0
  • php简单使用shmop函数创建共享内存减少服务器负载

    摘要:请注意,此函数返回一个编号,其他函数可使用该编号操作该共享内存段。从内存段读取数据从共享内存段读取数据很简单。函数将该内存段标记为删除,阻止任何其他进程打开它。 在之前的一篇博客[了解一下共享内存的概念及优缺点]已经对共享内存的概念做了说明。下面就来简单使用共享内存(其实也可以用其他工具,比如redis) PHP做内存共享有两套接口。一个是shm,它实际上是变量共享,会把对象变量序列化...

    smartlion 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<