使用opcache在PHP中缓存静态数据 [英] caching static data in PHP with opcache

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

问题描述

我有〜100个字符串变量,需要在PHP站点的每个网页上使用.数据永远不会在运行时更改,尽管将来我将需要多组数据并在用于页面请求的一组数据之间切换.字符串的长度从5到600个字符不等.我目前正在包含一个具有如下数据的文件:

I have ~100 string variables that need to be available on every webpage of a PHP site. The data will never change at runtime, though in the future I will need multiple sets of the data and to switch between the one in use for a page request. The length of the strings vary from 5 to 600 characters. I'm currently including a file that has the data like this:

$someStuff = "abc";
$otherStuff = "def";
// etc

我正在使用opcache.这种方法会从opcache中受益多少?

I am using opcache. How much will this approach benefit from opcache?

我已经看到了此答案.如果缓存的好处值得进行关键查找,那么我可以更改为使用关联数组.但是,对于我来说,使用带静态数组字段的类比声明变量更好是否适合我还是不清楚.

I've seen this answer. I could change to using an associative array if the caching benefits were worth doing key lookups. However, it isn't clear to me if using a class with a static array field is better for my situation than declaring variables.

也许带有静态变量的函数是一个好主意?这比静态类字段好还是坏?

Maybe a function with a static variable is a good idea? Is this the same, better or worse than a static class field?

function getItem ($name) {
    static $items = array("someStuff" => "abc");
    return $items[$name];
}

每个字符串可能是函数而不是变量?如果不是所有字符串都用于给定页面(通常是这种情况),这样做会更好吗?

Maybe a function instead of a variable for each string? Would this be better if not all the strings are used for a given page (which is often the case)?

function someStuff () { return "abc"; }
function otherStuff () { return "def"; }

什么是最佳解决方案?每个页面上都需要数据,因此我想尽可能地提高效率,避免从磁盘/数据库中读取数据等.

What is the best solution? The data is needed on every page so I would like to be as efficient as possible, avoid reading from disk/database, etc.

推荐答案

在实践中,是否执行以下操作都没有区别:

In practice it makes no difference whether you do something like:

$someStuff = "abc";
$otherStuff = "def";
// ...

$constants = array(
    'someStuff' => "abc";
    'otherStuff' = >"def";
    // ...
);

或按照我的其他答案将其包装到类的静态数组中.使用OPcache将消除编译开销和磁盘I/O开销.它将内联字符串常量,以便Zend引擎可以有效地静态使用它们.该类版本只对数组结构进行一个浅表复制,另外两个版本将执行〜200个操作码执行程序以初始化〜100个变量,并且实际的内在字符串也可以通过引用有效地复制.引擎通常每秒可解释20-40M个操作码执行程序,因此也可以进行数学运算:这无关紧要.

or wrapping this into a static array in a class as per my other answer. Using OPcache will remove the compile overheads and disk I/O overheads. It will intern the string constants so these are effectively statically available to the Zend engine. The class version does a single shallow copy of the array structure, the two other versions will do ~200 opcode execs to initialise ~100 variables, and again the actual interned strings are effectively copied by reference. The engine typically interprets 20-40M opcode execs per second, so do the math: it doesn't matter.

我的建议是:不必担心此处的运行时问题,只需选择您认为最清晰,最可维护的方法即可.就个人而言,我会使用从其自己的配置类文件中自动加载到的类,但这是您的应用程序,为您做最清楚的事情.

My recommendation is: don't worry about the runtime issues here, just pick the approach which you feel is clearest and most maintainable. Personally, I would use a class autoloaded into from its own config class file, but this is your app and do what is the clearest for you.

顺便说一句,使用函数很麻烦,并且确实要花费运行时间,并且函数调用是最昂贵的PHP操作之一.引用类常量要便宜很多,但是请自己对它们进行基准测试.但是,再次重申,除非您每次请求都提到这10k次以上,否则您不会注意到任何实质性差异.太聪明了,最终只会让你脚踏实地". :-)

BTW, using functions is messy and they do have a runtime cost, and function calls are one of the most expensive PHP operations. References to class constants are quite a lot cheaper, but try benchmarking these yourself. However, again, unless you are referring to these 10k + times per request you aren't going to notice a material difference. Being too clever will only end up with you "shooting yourself in the foot." :-)

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

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