php部分缓存 [英] Php Partial Caching

查看:95
本文介绍了php部分缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想部分缓存一些php文件。例如

I want caching some php files partially. for example

<?
echo "<h1>",$anyPerdefinedVarible,"</h1>";
echo "time at linux is: ";
// satrt not been catched section
echo date();
//end of partial cach
echo "<div>goodbye $footerVar</div>";
?>

所以缓存的页面应该像
(cached.php)

So cached page should be like as (cached.php)

<h1>This section is fixed today</h1>
<? echo date(); ?>
<div>goodbye please visit todays suggested website</div>

可以用模板完成,但我直接想要。因为我想要替代解决方案。

It may be done with templating but I want it directly. Because I want alternative solution.

推荐答案

看看php的ob_start(),它可以缓冲所有输出并保存。
http://php.net/manual/en/function.ob -start.php

Look at php's ob_start(), it can buffer all output and save this. http://php.net/manual/en/function.ob-start.php

添加:
参见 http://www.php.net/manual/zh/function.ob-start.php#106275 中的所需功能:)
编辑:
这是一个甚至simpeler的版本: http://www.php.net/manual/zh/function.ob-start.php#88212 :)

Addition: Look at http://www.php.net/manual/en/function.ob-start.php#106275 for the function you want :) Here a even simpeler version: http://www.php.net/manual/en/function.ob-start.php#88212 :)

这里有一些简单但有效的解决方案:

Here some simple, but effective, solution:

template.php

template.php

<?php
    echo '<p>Now is: <?php echo date("l, j F Y, H:i:s"); ?> and the weather is <strong><?php echo $weather; ?></strong></p>';
    echo "<p>Template is: " . date("l, j F Y, H:i:s") . "</p>";
    sleep(2); // wait for 2 seconds, as you can tell the difference then :-)
?>

actualpage.php

actualpage.php

<?php    
    function get_include_contents($filename) {
        if (is_file($filename)) {
            ob_start();
            include $filename;
            return ob_get_clean();
        }
        return false;
    }

    // Variables
    $weather = "fine";

    // Evaluate the template (do NOT use user input in the template, look at php manual why)
    eval("?>" . get_include_contents("template.php"));
?>

您可以使用 http://php.net/manual/en/function.file-put-contents.php 到某些文件,例如cached.php。然后,您可以让actualpage.php检查cached.php的日期,如果太旧了,则让它重新创建一个,或者如果它够年轻,只需回显actualpage.php或重新评估template.php,而无需重建模板。

You could save the contents of template.php or actualpage.php with http://php.net/manual/en/function.file-put-contents.php to some file, like cached.php. Then you can let the actualpage.php check the date of cached.php and if too old, let it make a new one or if young enough simply echo actualpage.php or re-evaluate template.php without rebuilding the template.

注释后,此处用于缓存模板:

After comments, here to cache the template:

<?php    
    function get_include_contents($filename) {
        if (is_file($filename)) {
            ob_start();
            include $filename;
            return ob_get_clean();
        }
        return false;
    }

    file_put_contents("cachedir/cache.php", get_include_contents("template.php"));

?>

要运行此文件,您可以直接运行缓存的文件,也可以将其包含在其他页面中。像这样:

To run this you can run the cached file directly, or you can include this on an other page. Like:

<?php
    // Variables
    $weather = "fine";

    include("cachedir/cache.php");
?>

这篇关于php部分缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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