Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner data caching in PHP

I'm using simpleXML to go through the XML results of a Twitter XML file, but I'm completely lost as to caching the results with PHP. This article seems to be of some help, but I've come across memcache (and memcached. C'mon, namers.) as well, and I have no idea what to do.

I'm using this:

$sxml = simplexml_load_file(
    'http://api.twitter.com/1/qworky/lists/qworkyteam/statuses.xml');

foreach($sxml->status as $status){
    $name = $status->user->name;
    $image = $status->user->profile_image_url;
    $update = $status->text;
    $url = "http://twitter.com/" . $status->user->screen_name;
}

to simply store the XML data of a Twitter list into usable variables. But what's the right thing to do? Create a cache file and only run this block of PHP if the cache file is older than ten minutes, otherwise serve up the cached variables? How do I pass the cached variables back-and-forth between the cached file and the DOM? Heck, what kind of extension and filename does a cache file have?

Thanks so much for any way you can point me in a healthy direction here.

like image 859
Joshua Cody Avatar asked Feb 17 '10 08:02

Joshua Cody


People also ask

Can PHP be cached?

PHP basically has two main types of caching: 'output caching' and 'parser caching'. PHP 'output caching' saves a chunk of data somewhere that can later be read by another script faster than it can generate it. 'Parser caching' is specific feature.

How do I cache a PHP file?

php // Cache the contents to a cache file $cached = fopen($cachefile, 'w'); fwrite($cached, ob_get_contents()); fclose($cached); ob_end_flush(); // Send the output to the browser ?> If a cached file named $cachefile isn't found on your server, this code will be executed and will create the cache file itself.

What are the two types of caching?

L1 cache, or primary cache, is extremely fast but relatively small, and is usually embedded in the processor chip as CPU cache. L2 cache, or secondary cache, is often more capacious than L1.

Do browsers cache PHP files?

I came to the conclusion that, by default, php files are never EVER pulled from cache, even in mobile browsers, even if in the response there is no Cache-Control nor Expires parameter, even if i don't send POST requests and i just follow a link to the page. They are always redownloaded.


1 Answers

As a general concept, caching doesn't imply a strategy for implementation. The prevalent idea is that you store the information somewhere that provides you more efficent access than where you obtained the data from originally.

So in this case, it's more efficient to get the data from the disk than it is to requery Twitter (generally, network latency is greater than disk IO latency).

Also, getting data from memory is more efficient than getting the information from disk (because memory latency is less than disk IO latency).

That being said, you can store the values from Twitter in memory, if you wish, or to a file on disk, if you need the values to persist beyond say, a shutdown. How you do it is up to you (disk or memory, extensions, format, etc, etc). It's your cache.

The thing you have to be careful of is the cache growing stale. This is when the information that you have in your cache is out of sync with the original data source. You have to make the determination for your application how acceptable stale data is, and requery as appropriate, replacing your cache values.

like image 59
casperOne Avatar answered Oct 20 '22 05:10

casperOne