caching static data in PHP with opcache

佐手、 提交于 2020-01-05 05:26:48

问题


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

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.


回答1:


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

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

or

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

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.

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." :-)



来源:https://stackoverflow.com/questions/23169454/caching-static-data-in-php-with-opcache

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!