With the new version of the Last.fm nationality statistics I’m scanning the content of the user’s whole music library. Since this is a task which needs a lot of time I needed to cache the result. First, I thought of caching it in the database, but as the cached data is a bit bigger and sending the data from the harddisk to the database server and then to the script would be too much overhead. I implemented a simple file caching algorithm.

My version could be easily used as you only need to supply the filename where the method result could be already be cached (the default lifetime of a cached result is one week) and the method that should be called if the cached result is outdated as a block. The following code shows how my algorithm is implemented and how it can be used: