From scratch, here my 2 cents for a 24h rolling cache. it certainly can be improved
If a such improvement already exists, forget about it.
You add a boolean GET "cache", if cache=1, then you call the cache file, otherwise, you keep the current script (with or without the current limitation)
in the api.php
- Code: Select all
$cache_name = md5($_SERVER['REQUEST_URI']); // so each type of api call has is own cache file, depending of the GET values (1)
if ($_GET('cache') && file_exists('cache_dir/'.$cache_name)) { // if the user accept the cache and if the cache file exist
include('cache_dir/'.$cache_name); // return the cache file content
}else{
/*** here the current api.php which return also the generated xml ***/
$fp = fopen('cache_dir/'.$cache_name, 'w+'); // create/open the cache file
fputs($fp,$xml); // write the cache file content
}
and you add a cron job, which delete all cache file older than 24h, via a script bash
(1)
It can be improved there, as the name will depend of the GET values order, and have two different cache file, even if the returned xml is the same (ex : api.php?cache=1&mode=gamelist and api.php?mode=gamelist&cache=1)
i also thought of a RegExp that create the file name by deleting all forbidden special character in file name from the REQUEST_URI, but it could be a problem for two players named ' foo?bar ' and ' foo/bar ' which give the same name. Anyway, a RegExp should be better