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 = ; /**
* 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", , );
}
} /**
* 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", , );
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, );
} else {
$this->shmid = shmop_open($this->id, "c", $this->perms, $size);
shmop_write($this->shmid, $data, );
}
} /**
* 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, , $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();
$this->assertInstanceOf('Simple\\SHM\\Block', $memory);
$this->assertEquals(, $memory->getId()); $memory->write('Sample 2');
$data = $memory->read();
$this->assertEquals('Sample 2', $data);
} public function testIsMarkingBlockForDeletion()
{
$memory = new Block();
$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();
$new->write('Sample');
echo $new->read(); ?>

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

<?php

require('SimpleSHM.class.php');

$results = array(
'user' => 'John',
'password' => '',
'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类及简单使用测试(代码)的更多相关文章

  1. Linux进程IPC浅析[进程间通信SystemV共享内存]

    Linux进程IPC浅析[进程间通信SystemV共享内存] 共享内存概念,概述 共享内存的相关函数 共享内存概念,概述: 共享内存区域是被多个进程共享的一部分物理内存 多个进程都可把该共享内存映射到 ...

  2. php简单使用shmop函数创建共享内存减少服务器负载

    在之前的一篇博客[了解一下共享内存的概念及优缺点]已经对共享内存的概念做了说明.下面就来简单使用共享内存(其实也可以用其他工具,比如redis) PHP做内存共享有两套接口.一个是shm,它实际上是变 ...

  3. PHP共享内存的应用shmop系列

    简单的说明 可能很少情况会使用PHP来操控共享内存,一方面在内存的控制上,MC已经提供了一套很好的方式,另一方面,自己来操控内存的难度较大,内存的读写与转存,包括后面可能会用到的存储策略,要是没有一定 ...

  4. System V IPC 之共享内存

    IPC 是进程间通信(Interprocess Communication)的缩写,通常指允许用户态进程执行系列操作的一组机制: 通过信号量与其他进程进行同步 向其他进程发送消息或者从其他进程接收消息 ...

  5. PHP共享内存详解

    前言 在PHP中有这么一族函数,他们是对UNIX的V IPC函数族的包装. 它们很少被人们用到,但是它们却很强大.巧妙的运用它们,可以让你事倍功半. 它们包括: 信号量(Semaphores) 共享内 ...

  6. Linux 程序设计1:深入浅出 Linux 共享内存

    笔者最近在阅读Aerospike 论文时,发现了Aerospike是利用了Linux 共享内存机制来实现的存储索引快速重建的.这种方式比传统利用索引文件进行快速重启的方式大大提高了效率.(减少了磁盘 ...

  7. C 共享内存封装

    引言 - 背景 2016 年写过一篇关于 linux 共享内存 shm api 扫盲文. C扩展 从共享内存shm到memcache外部内存 比较简单. 没有深入分析(能力有限, 也深入分析不了). ...

  8. PHP共享内存

    如何使用 PHP shmop 创建和操作共享内存段,使用它们存储可供其他应用程序使用的数据. 1. 创建内存段 共享内存函数类似于文件操作函数,但无需处理一个流,您将处理一个共享内存访问 ID.第一个 ...

  9. Akka系列(四):Akka中的共享内存模型

    前言...... 通过前几篇的学习,相信大家对Akka应该有所了解了,都说解决并发哪家强,JVM上面找Akka,那么Akka到底在解决并发问题上帮我们做了什么呢? 共享内存 众所周知,在处理并发问题上 ...

随机推荐

  1. ASP.Net中的四种状态保持机制

    每个人上网可多有过这样的情况,当我们登陆某个网站时,在登陆的旁边会有一个 "记住我" 的复选框,有的网站还会让用户选择记住我.这个记住我是怎么实现的呢? 其实就用利用的是cooki ...

  2. 【转】JsonPath教程

    https://blog.csdn.net/koflance/article/details/63262484 1. 介绍 类似于XPath在xml文档中的定位,JsonPath表达式通常是用来路径检 ...

  3. js使用的一些实用技巧

    1.jquery中页面定时调用ajax方法 function SetContinueSend(param1,param2, func){ func.call(null,param1,param2) } ...

  4. datetime函数和random.seed()函数的应用

    一,datetime 在python中datetime是一个库是一个模块也是一个函数,作用很多,这里面只对其做简单的最常用的讲解. 首先返回系统时间 import datetime nowTime=d ...

  5. 解读经典面试题for循环console.log

    for (var i = 1; i <= 5; i++) { setTimeout(function () { console.log(i) },1000)} 会打印出5个6,这是why 因为 ...

  6. caffe配置文件

    一.数据层及参数 要运行caffe,需要先创建一个模型(model),如比较常用的Lenet,Alex等, 而一个模型由多个屋(layer)构成,每一屋又由许多参数组成.所有的参数都定义在caffe. ...

  7. MyBatis基础入门《十三》批量新增数据

    MyBatis基础入门<十三>批量新增数据 批量新增数据方式1:(数据小于一万) xml文件 接口: 测试方法: 测试结果: =============================== ...

  8. web前端 ajax请求报415/400错

    一.当使用jQuery 的ajax发送请求时 1.请求路劲没有出错并且不存在跨域,出现415错误 解决方法:在ajax请求中加上contentType: 'application/json', $.a ...

  9. jQuery-动画点击淡化消失

    <!DOCTYPE html> <html> <head> <title> new document </title> <meta c ...

  10. Unity shader学习之Forward Rendering Path

    Forward rendering path shader如下: // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObje ...