Churchill
Churchill

Reputation: 1607

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 repeated calls to http://www.mysite.com/common/footer.asp

Cheers

Upvotes: 1

Views: 115

Answers (2)

txyoji
txyoji

Reputation: 6848

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.

Upvotes: 0

Long Ears
Long Ears

Reputation: 4896

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.

Upvotes: 3

Related Questions