静态数组是否存储在opcache中? [英] Do static arrays get stored in opcache?

查看:91
本文介绍了静态数组是否存储在opcache中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个很大的 100k 元素关联数组,如下所示:

Say I have a rather large associative array of 100k elements like so:

$resources = array(
'stone'=>'current_stone.gif',
'stick'=>'uglystick.jpg',
...
);

存储在名为 resources.php 的文件中,该文件在运行时永远不会更改.

Stored in a file called resources.php and it will never change at runtime.

我希望将此数据存储在Zend opcache中,以便在所有进程之间共享(节省内存)并可能加快查找速度.

I would like to have this data in Zend opcache so as to share it across all processes (saving memory) and possibly speed up the lookup speed.

我目前的假设是,以这种形式,此数组将不会存储在opcache中,因为它没有在任何地方定义为静态结构.

My current assumption is that, in this form, this array will not get stored in opcache as it's not defined as a static structure anywhere.

我将如何确保这些数据进入opcache?

How would I go about making sure this data gets into opcache?

推荐答案

不,您不能在OPcache中存储变量,但是类中的静态变量可以工作:

No you can't store variables in OPcache, but statics in classes work:

class Resource {
    static $image = [
        'stone'=>'current_stone.gif',
        'stick'=>'uglystick.jpg',
        ...
    ];
}
...
echo Resource::$image['stone'], "\n";

这将保存所有初始化数组的操作码,但是OPcache仍将SMA编译脚本中的Resource :: $ image版本深复制到进程空间中的相应类静态属性中,因此您仍然会有一个每个正在使用Resource的活动进程中HashTable的副本-尽管字符串本身将被 interned 并因此在所有使用此类的活动php请求中共享.

This saves all of the opcodes initialising the arrays, but OPcache will still deep copy the version of Resource::$image in the compiled script in SMA into the corresponding class static property in the process space, so you will still have a copy of the HashTable in each of the active processes which are using Resource -- though the strings themselves will be interned and hence shared across all active php requests which are using this class.

如果您正在使用类自动加载器来加载您的类,则除了引用Resoure::$image...之外,您无需执行其他任何操作,自动加载器将为您完成映射.

If you are using a class autoloader, to load your classes, then you don't even need to do anything other than refer to Resoure::$image... and the autoloader will do the mapping for you.

这篇关于静态数组是否存储在opcache中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆