Home » Php » Beginner data caching in PHP

Beginner data caching in PHP

Posted by: admin July 12, 2020 Leave a comment


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(

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.

How to&Answers:

Joshua take a look at this article about simple PHP caching, you can do exactly as you’ve described with relative ease (and yes is probably the most sensible way to go).


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.