开发者

PHP Caching Question

I recently worked on a project on .NET and part of the code involved reading the header and footer from a pre-defined format for consistency across various websites shared by the same client.

I am faced by a similar problem in php. I get the contents of the header/footer links using:

<?php
$contents = file_get_contents('http://www.mysite.com/common/footer.asp');
echo $contents;
?>

Is there a way I can load this into cache to avoid repeate开发者_StackOverflowd calls to http://www.mysite.com/common/footer.asp

Cheers


You can use APC's caching if you have the extension available:

$contents = apc_fetch('footer');
if (!$contents) {
    $contents = file_get_contents('http://www.mysite.com/common/footer.asp');
    apc_store('footer', $contents);
}

If you want to use the same cache across different machines or don't have APC available then memcached could be used in a very similar fashion.


If your looking to cache across many page loads, you could use the session.

session_start();
$_SESSION['somekey'] = value;

Or into persistent storage like a flat file, database or memcache.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜