How do I cache a web page in PHP?

2019-03-10 00:30发布

how do I cache a web page in php so that if a page has not been updated viewers should get a cached copy?

Thanks for your help. PS: I am beginner in php.

标签: php caching
6条回答
霸刀☆藐视天下
2楼-- · 2019-03-10 01:10
$c = ob_get_contents();
file_put_contents($cachefile);

correct is

$c = ob_get_contents();
file_put_contents($cachefile,$c);

otherwise the script will not work.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-10 01:17

Additionally to mauris answer I'd like to point this out:

You have to be careful when you use caching. When you have dynamic data (which should be the case when you use php instead of static html) then you have to invalidate the cache when the corresponding data changes.

This can be pretty easy or extremely tricky, depending on your kind of dynamic data.

Update

How you invalidate the cache depends on the concrete kind of caching. You have to know which cache files belong to which page (and maybe with user input). When the data changes you should delete the cached file or remove the page output from your cache data structure.

I can't give you any more detailed info about that without knowing which implementation you use for caching.

Other folks suggested for example the Pear package or memcached. These have the necessary functions to invalidate the whole cache or parts of it when data changes.

查看更多
何必那么认真
4楼-- · 2019-03-10 01:21

PEAR has a caching package (actually two):

http://pear.php.net/package/Cache and
http://pear.php.net/package/Cache_Lite for smaller apps

I once used the Cache package (first one) for query caching and at that time it did its work as far as I remember.

查看更多
虎瘦雄心在
5楼-- · 2019-03-10 01:30

Use Squid or update the HTTP headers correctly to do browser caching. I don't see the need to spin your own version of caching based on the question.

查看更多
够拽才男人
6楼-- · 2019-03-10 01:34

You can actually save the output of the page before you end the script, then load the cache at the start of the script.

example code:

<?php

$cachefile = 'cache/'.basename($_SERVER['PHP_SELF']).'.cache'; // e.g. cache/index.php.cache
$cachetime = 3600; // time to cache in seconds

if(file_exists($cachefile) && time()-$cachetime <= filemtime($cachefile)){
  $c = @file_get_contents($cf);
  echo $c;
  exit;
}else{
  unlink($cachefile);
}

ob_start();

// all the coding goes here

$c = ob_get_contents();
file_put_contents($cachefile);

?>

If you have a lot of pages needing this caching you can do this:

in cachestart.php:

<?php
$cachefile = 'cache/'.basename($_SERVER['PHP_SELF']).'.cache'; // e.g. cache/index.php.cache
$cachetime = 3600; // time to cache in seconds

if(file_exists($cachefile) && time()-$cachetime <= filemtime($cachefile)){
  $c = @file_get_contents($cf);
  echo $c;
  exit;
}else{
  unlink($cachefile);
}

ob_start();
?>

in cacheend.php:

<?php

$c = ob_get_contents();
file_put_contents($cachefile);

?>

Then just simply add

include('cachestart.php');

at the start of your scripts. and add

include('cacheend.php');

at the end of your scripts. Remember to have a folder named cache and allow PHP to access it.

Also do remember that if you're doing a full page cache, your page should not have SESSION specific display (e.g. display members' bar or what) because they will be cached as well. Look at a framework for specific-caching (variable or part of the page).

查看更多
够拽才男人
7楼-- · 2019-03-10 01:35

Use memcached. There's an explanation of how to do it on that site.

查看更多
登录 后发表回答