Beginner data caching in PHP

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.