缓存包括PHP中的迭代重用 [英] Caching includes in PHP for iterated reuse

查看:140
本文介绍了缓存包括PHP中的迭代重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法有效地缓存PHP include 以便重用,没有APC,等等?

Is there a way to cache a PHP include effectively for reuse, without APC, et al?

简单(虽然是愚蠢的)示例:

// rand.php
return rand(0, 999);

// index.php
$file = 'rand.php';
while($i++ < 1000){
    echo include($file);
}

同样,虽然荒谬,但这对脚本会转储1000个随机数。但是,对于每次迭代,PHP必须访问文件系统(正确?没有遗漏的缓存功能我错过了,是吗?

Again, while ridiculous, this pair of scripts dumps 1000 random numbers. However, for every iteration, PHP has to hit the filesystem (Correct? There is no inherit caching functionality I've missed, is there?)

基本上,我怎样才能阻止上一个场景导致文件系统的1000次点击?

Basically, how can I prevent the previous scenario from resulting in 1000 hits to the filesystem?

到目前为止我唯一考虑的是一个傻瓜,并且它可能根本没有效果(没有测试过,在这里写过,容易出错,但你明白了):

The only consideration I've come to so far is a goofy one, and it may not prove effective at all (haven't tested, wrote it here, error prone, but you get the idea):

// rand.php
return rand(0, 999);

// index.php
$file = 'rand.php';
$cache = array();
while($i++ < 1000){
    if(isset($cache[$file])){
        echo eval('?>' . $cache[$file] . '<?php;');
    }else{
        $cache[$file] = file_get_contents($file);
        echo include($file);
    }
}






一个更现实,更不愚蠢的例子:

当包含用于生成视图的文件时,给定视图文件在给定的情况下被多次使用请求(小部件或其他东西)是否有一种现实的方法来捕获和重新评估视图脚本而没有文件系统命中?

When including files for view generation, given a view file is used a number of times in a given request (a widget or something) is there a realistic way to capture and re-evaluate the view script without a filesystem hit?

推荐答案

如果通过网络访问包含文件,这只会有意义。

This would only make any sense if the include file was accessed across a network.


我没有遗漏任何继承缓存功能,是吗?

There is no inherit caching functionality I've missed, is there?

所有操作系统都经过高度优化,可以减少物理I / O的数量并加快文件操作。在大多数情况下,在正确配置的系统上,系统很少会恢复到磁盘以获取PHP代码。坐下来用电子表格思考如果每个文件必须从磁盘中取出,处理PHP代码需要多长时间 - 这很荒谬,例如:假设您的脚本位于/var/www/htdocs/index.php并包含/usr/local/php/resource.inc.php - 这是8个寻找操作来定位文件 - 每个@ 8ms,这是64ms来查找文件!在你的测试用例上运行一些时间 - 你会发现它的运行速度远远超过它。

All operating systems are very highly optimized to reduce the amount of physical I/O and to speed up file operations. On a properly configured system in most cases, the system will rarely revert to disk to fetch PHP code. Sit down with a spreadsheet and have a think about how long it would take to process PHP code if every file had to be fetched from disk - it'd be ridiculous, e.g. suppose your script is in /var/www/htdocs/index.php and includes /usr/local/php/resource.inc.php - that's 8 seek operations to just locate the files - @8ms each, that's 64ms to find the files! Run some timings on your test case - you'll see that its running much, much faster than that.

这篇关于缓存包括PHP中的迭代重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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