Lightweight external resource caching and concatenation. Particularly useful for decreasing external API requests (especially paginated JSON APIs) while having granular control over expiration. No setup required: automatically creates cache subdirectory and deletes expired files by checking expiration threshold with each request.
Just drop the index.php
file onto your server (preferably in its own directory), make sure permissions are set, and start making requests!
url
A fully formed URL. This can take a comma-separated list of targets to cache and combine into one output (appended to one another). Pass complex addresses as percent-encoded.
expire
Time in seconds until cached file expires (3600 for one minute, 216000 for an hour, etc).
cache/?url=http://swapi.co/api/starships/9/?format=json&expire=3600
json
Concatenate output if target resources are JSON formatted.
direct
Enable a 302 redirect to the target resource (for the first passed URL). Useful for temporary bypasses of caching during testing.
errors
Include any errors in output. Useful for debugging.
cache/?url=http://swapi.co/api/starships/9/?format=json, http://swapi.co/api/people/4/?format=json &expire=21600
cache/?url=http%3A%2F%2Fswapi.co%2Fapi%2Fplanets%2F%3Fformat%3Djson%26page%3D1, http%3A%2F%2Fswapi.co%2Fapi%2Fplanets%2F%3Fformat%3Djson%26page%3D2, http%3A%2F%2Fswapi.co%2Fapi%2Fplanets%2F%3Fformat%3Djson%26page%3D3 &expire=518400&json
(URL encoded because the passed URLs themselves have parameters.)
cache/?url=http://swapi.co/api/films/1/?format=json&expire=3600&direct
- Set expires header to match expiry threshold.
- Allow cache directory to be configurable.
- Add
.cache
file extension for security. - Explore a flag for a 301 rediret to single local file versus loading it in with
file_get_contents()
. - Evaluate if there's a better way to cache combined and concatenated files.
- Investigate if there's a better way to match files other than
glob()
.