简体   繁体   中英

page caching using php

我正在寻求所有人的指导,他们可以告诉我有关网站的页面缓存...我在php工作,所以如果有人可以解释我如何在PHP中执行缓存。

PHP offers an extremely simple solution to dynamic caching in the form of output buffering. The front page of the site (which generates by far the most traffic) is now served from a cached copy if it has been cached within the last 5 minutes.

<?php

  $cachefile = "cache/".$reqfilename.".html";
  $cachetime = 5 * 60; // 5 minutes

  // Serve from the cache if it is younger than $cachetime
  if (file_exists($cachefile) && (time() - $cachetime
     < filemtime($cachefile))) 
  {
     include($cachefile);
     echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." 
     -->n";
     exit;
  }
  ob_start(); // start the output buffer
?>

.. Your usual PHP script and HTML here ...

<?php
   // open the cache file for writing
   $fp = fopen($cachefile, 'w'); 

   // save the contents of output buffer to the file
    fwrite($fp, ob_get_contents());

    // close the file

    fclose($fp); 

    // Send the output to the browser
    ob_end_flush(); 
?>

This is a simple cache type,

you can see it here

http://www.theukwebdesigncompany.com/articles/php-caching.php

You can use Smarty has cache technique

http://www.nusphere.com/php/templates_smarty_caching.htm

I'm rather surprised that none of the responses so far seem to have addressed the possibility of caching anywhere OTHER than on the server where PHP is running.

There's a lot of functionality within HTTP to allow proxies and browsers to re-use content previously supplied without having to refer back to the origin. So much so that I wouldn't even try to answer this in a SO reply.

See this tutorial for a good introduction to the topic.

C.

Here's a useful link for you, regarding the basics of caching and how to apply that with php.

http://www.devshed.com/c/a/PHP/Output-Caching-with-PHP/

Keep in mind in most cases proper caching should apply earlier (aka the request doesn't even reach the php script).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM