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中文网!
相关推荐:
以上がPHP は共有メモリの shmop クラスと簡単な使用テスト コードを操作します。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Linuxは、豊富なツールとコミュニティサポートを提供するネイティブプラットフォームであるため、LinuxでDockerが重要です。 1. docker:sudoapt-getupdateとsudoapt-getinstalldocker-cedocker-ce-clicotainerd.ioを使用します。 2。コンテナの作成と管理:Dockerrun-D-Namemynginx-P80:80NginxなどのDockerrunコマンドを使用します。 3。DockerFileを書き込み:画像サイズを最適化し、マルチステージ構造を使用します。 4。最適化とデバッグ:DockerLogsとDockerexを使用します

IISとPHPは互換性があり、FastCGIを通じて実装されています。 1..phpファイル要求を構成ファイルを介してFastCGIモジュールに転送します。 2. FASTCGIモジュールは、PHPプロセスを開始して、パフォーマンスと安定性を改善するための要求を処理します。 3。実際のアプリケーションでは、構成の詳細、エラーデバッグ、パフォーマンスの最適化に注意する必要があります。

session_start()への複数の呼び出しにより、警告メッセージと可能なデータ上書きが行われます。 1)PHPは警告を発し、セッションが開始されたことを促します。 2)セッションデータの予期しない上書きを引き起こす可能性があります。 3)session_status()を使用してセッションステータスを確認して、繰り返しの呼び出しを避けます。

CでのハイDPIディスプレイの取り扱いは、次の手順で達成できます。1)DPIを理解してスケーリングし、オペレーティングシステムAPIを使用してDPI情報を取得し、グラフィックスの出力を調整します。 2)クロスプラットフォームの互換性を処理し、SDLやQTなどのクロスプラットフォームグラフィックライブラリを使用します。 3)パフォーマンスの最適化を実行し、キャッシュ、ハードウェアアクセラレーション、および詳細レベルの動的調整によりパフォーマンスを改善します。 4)ぼやけたテキストやインターフェイス要素などの一般的な問題を解決し、DPIスケーリングを正しく適用することで解決します。

CのDMAとは、直接メモリアクセステクノロジーであるDirectMemoryAccessを指し、ハードウェアデバイスがCPU介入なしでメモリに直接データを送信できるようにします。 1)DMA操作は、ハードウェアデバイスとドライバーに大きく依存しており、実装方法はシステムごとに異なります。 2)メモリへの直接アクセスは、セキュリティリスクをもたらす可能性があり、コードの正確性とセキュリティを確保する必要があります。 3)DMAはパフォーマンスを改善できますが、不適切な使用はシステムのパフォーマンスの低下につながる可能性があります。実践と学習を通じて、DMAを使用するスキルを習得し、高速データ送信やリアルタイム信号処理などのシナリオでその効果を最大化できます。

MacOSANDLINUXBOTHEFFERUNIQUESTRENGSS:MacOSProvidesAuser-FriendlyExperienceとExcellenthardWareIntegration、Whilelinuxexcelsinfexibility andCommunitySupport.macos、decondbyApple、sleekinterfaceStemation、whiseedlinuxを使用しています

AIは、作曲家の使用を最適化するのに役立ちます。特定の方法には次のものが含まれます。1。依存関係管理の最適化:AIは依存関係を分析し、最適なバージョンの組み合わせを推奨し、競合を減らします。 2。自動コード生成:AIは、ベストプラクティスに準拠したComposer.jsonファイルを生成します。 3.コードの品質を改善する:AIは潜在的な問題を検出し、最適化の提案を提供し、コードの品質を向上させます。これらの方法は、開発者が効率とコードの品質を向上させるのに役立つ機械学習および自然言語処理技術を通じて実装されています。

Centosは、優れたセキュリティ、安定性、パフォーマンスのために、サーバー環境とエンタープライズ環境にとって最初の選択肢です。 1)セキュリティは、システムセキュリティを改善するために、SELINUXを介した強制アクセス制御を提供します。 2)システムの安定性を確保するために、安定性はLTSバージョンによって最大10年間サポートされています。 3)パフォーマンスにより、カーネルとシステムの構成を最適化することにより、システムの応答速度とリソースの使用率が大幅に向上します。
