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操作共享内存shmop类及简单使用测试(代码)的详细内容。更多信息请关注PHP中文网其他相关文章!

PHP用于构建动态网站,其核心功能包括:1.生成动态内容,通过与数据库对接实时生成网页;2.处理用户交互和表单提交,验证输入并响应操作;3.管理会话和用户认证,提供个性化体验;4.优化性能和遵循最佳实践,提升网站效率和安全性。

PHP在数据库操作和服务器端逻辑处理中使用MySQLi和PDO扩展进行数据库交互,并通过会话管理等功能处理服务器端逻辑。1)使用MySQLi或PDO连接数据库,执行SQL查询。2)通过会话管理等功能处理HTTP请求和用户状态。3)使用事务确保数据库操作的原子性。4)防止SQL注入,使用异常处理和关闭连接来调试。5)通过索引和缓存优化性能,编写可读性高的代码并进行错误处理。

在PHP中使用预处理语句和PDO可以有效防范SQL注入攻击。1)使用PDO连接数据库并设置错误模式。2)通过prepare方法创建预处理语句,使用占位符和execute方法传递数据。3)处理查询结果并确保代码的安全性和性能。

PHP和Python各有优劣,选择取决于项目需求和个人偏好。1.PHP适合快速开发和维护大型Web应用。2.Python在数据科学和机器学习领域占据主导地位。

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP可以轻松创建互动网页内容。1)通过嵌入HTML动态生成内容,根据用户输入或数据库数据实时展示。2)处理表单提交并生成动态输出,确保使用htmlspecialchars防XSS。3)结合MySQL创建用户注册系统,使用password_hash和预处理语句增强安全性。掌握这些技巧将提升Web开发效率。

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP仍然具有活力,其在现代编程领域中依然占据重要地位。1)PHP的简单易学和强大社区支持使其在Web开发中广泛应用;2)其灵活性和稳定性使其在处理Web表单、数据库操作和文件处理等方面表现出色;3)PHP不断进化和优化,适用于初学者和经验丰富的开发者。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版
中文版,非常好用

Dreamweaver Mac版
视觉化网页开发工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载
最流行的的开源编辑器