2

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>";
?>

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.

7
  • What have you tried? Commented Apr 1, 2012 at 22:25
  • Generating these lines will be faster, than fetching 2 keys from cache storage. Try to cache data from database, don't waste your time to output, it's template engine's business. Commented Apr 1, 2012 at 22:26
  • This code is only for example. Real codes are so complex and requires some sql queries. I try show my problem very clear. I want to know PHP caching mechanism. Commented Apr 1, 2012 at 22:32
  • Well, it's not only about PHP, it's common. Again: cache data, fetched from DB, don't cache output. And try to use Twig for templates, it will open your understanding of separation data from representation. Commented Apr 1, 2012 at 22:41
  • @OZ_ it is common case for all programming languages, but I know PHP lang better than others. I want use it for flexible templates. I need template that contains multiple parts that was created at different dates. Commented Apr 1, 2012 at 22:48

1 Answer 1

4

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

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


Here some simple, but effective, solution:

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

<?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"));
?>

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");
?>
Sign up to request clarification or add additional context in comments.

9 Comments

it is usefull, but it do cach all parts of page. I want cach some parts of php outputs not all of them.
You could pass it to a function, include/require it - it's flexible. Of course, you can pass your contents yourself to a cached file, look at fopen() and fwrite() function then - you'll still have to pass the content to them. You can use filemtime() to check the time/date of the file and simply replace it, if it becomes out of date.
I want use it for flexible templates. I need template that contains multiple parts that was created at different dates. Therefor I need any function that ignore specified php codes section from caching.
So make that function? You have all the information needed for it, if not please do say. Instead of echo "myVar"; you can make a function echoBuffer and so use echoBuffer("myVar"); for contents that must be buffered and make a function that returns your needs. (in this case is should return a (buffered) string "myVar". You can use them both; like echo, date, other functions etc.
You could also simply echo '<?php' . date() . '?>'; to your new template file, maybe that is what your looking for?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.