在所有 PHP 进程之间共享变量/内存
是否可以在所有 PHP 进程之间共享变量和数组而不重复?
使用 memcached,我认为 PHP 会重复使用的内存:$array = $memcache->get('array');
$array 将包含来自 memcached 的副本。
所以我的想法是,可能有一个已经定义的静态变量,并在所有进程之间共享。
Is it possible to share variables and arrays between all PHP processes without duplicating them?
Using memcached, I think PHP duplicates the used memory:$array = $memcache->get('array');
$array will contain a copy from memcached.
So my idea is, there could be a static variable that was already defined, and shared between all processes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用Shmop:
来自:http://www.php.net/manual/en/intro。 shmop.php
构建此扩展不需要外部库。
共享内存函数
基本用法
Using
Shmop
:from: http://www.php.net/manual/en/intro.shmop.php
No external libraries are needed to build this extension.
The shared Memory Functions
Basic usage
在 PHP 进程之间共享内存的一种方法是安装 PHP 字节码缓存,例如 APC。 APC 主要用于将字节码存储到操作系统管理的共享内存段中,但它也有一个 API,用于在进程之间共享您想要的任何内容(例如本地版本的 memcache)。
然后是其他地方:
共享内存的一个大问题是两个进程很容易互相踩对方的脚。因此,共享内存最适合那些不会改变太多的东西,比如大型全局数组。
One way to share memory between PHP processes is to install a PHP-bytecode cache like APC. APC is primarily used for storing the bytecode into an OS managed shared-memory segment, but it also has an API for sharing anything you want between processes (like a local version of memcache).
Then elsewhere:
The big problem with sharing-memory is that it becomes very easy for two processes to step on each other's foot. So shared memory is best for things that don't change too much, like big global arrays.
默认情况下这是不可能的。每个解决方案总是会将内容复制到当前作用域中,因为如果没有,则无法访问它。
我不知道,到底想做什么,但也许你可以“在外面”做,例如作为 gearman作业,然后只捕获该过程的结果,而不是整个数组。
您还可以考虑将“大”数组拆分为多个切片,然后始终从 apc 或 memcached 检索当前需要的部分。
By default its simply not possible. Every solution will always copy the content into the current scope, because if not, there is no way to access it.
I dont know, what exactly want to do, but maybe you can do that "outside", for example as a gearman job, and then just catch the results of the process, instead of the whole array.
You can also think about splitting the "big" array into slices and then always retrieve the part you currently need from an apc or memcached.
PHP 有一些神奇的方法:
__get($property)
让我们实现对对象 $property 的访问__set($property, $value)
让我们实现对对象的赋值对象上的 $propertyPHP 可以序列化变量:
serialize($variable)
返回变量的字符串表示unserialize($string)
从字符串PHP 可以处理文件,具有并发访问管理:fopen($file, 'c+')
打开一个启用咨询锁定选项的文件(允许您使用集群)flock($descriptor, LOCK_SH )
获取共享锁(用于读取)flock($descriptor, LOCK_EX)
获取独占锁(用于写入)因此,在应用程序之间共享对象的最简单方法是创建一个实现并使用所有这些内容将所有数据立即保存和恢复到文件中的类。
该类的一个简单实现可以是:
现在,您可以像
stdClass
一样使用此类,但在构造时使用文件路径。这个例子当然非常简单,它关心对文件的并发访问,但不关心对变量的并发访问,在更好的实现中,您将使用类似互斥锁的锁。
我刚刚在 github 上推送了这个课程(完成后),你可以在这里找到它 。
PHP has magic methods:
__get($property)
let us implement the access of a $property on an object__set($property, $value)
let us implement the assignation of a $property on an objectPHP can serialize variables:
serialize($variable)
returns a string representation of the variableunserialize($string)
returns back a variable from a stringPHP can handle files, with concurrent-access management:
fopen($file, 'c+')
opens a file with advisory lock options enabled (allow you to use flock)flock($descriptor, LOCK_SH)
takes a shared lock (for reading)flock($descriptor, LOCK_EX)
takes an exclusive lock (for writting)So, the easiest way to share an object between apps is to create a class that implements and use all those stuffs to save and restore instantly all its data into a file.
A simple implementation of that class could be :
Now, you can use this class like
stdClass
, but with a file path when constructing.This example is of course very simple, it takes care about concurrent access to a file but not to a variable, in a better implementation you'll use a mutex-like lock.
I just pushed this class (after completing it) on github, you can find it here.
编辑:
您可能以错误的方式使用共享内存。
你的共享内存本身就是这样的数组。因此,您必须将单独的多语言字符串直接存储在共享内存中,而不是使用大数组。
然后仅拉动特定页面所需的字符串。
就这样。
一般来说,要处理某些数据,程序必须通过将其存储在变量中来“复制”它。
这就是变量的用途 - 存储(或“复制”)一些外部数据。
例如,如果您的数据库中有一些用户信息,要在网页上显示用户名,您必须“复制”此数据,首先将其存储在 PHP 变量中。
等等。
你是第一个认为这种做法需要改变的人。
Edit:
You are probably using shared memory wrong way.
Your shared memory itself being such array. So you have to store separate multilanguage strings directly in shared memory, not big array with them.
and then only pull strings, required on particular page.
that's all.
In general, to process some data, a program have to "duplicate" it, by storing it in a variable.
That's what variables are for - to store (or "duplicate") some outside data.
For example, if you have some user info in your database, to display a username on a web page you have to "duplicate" this data, storing it in PHP variable first.
And so on.
You are first who thinks that such approach needs to be changed.