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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With