Skip to content
/ cache Public

Dead simple external resource caching (and concatenation).

Notifications You must be signed in to change notification settings

round/cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 

Repository files navigation

Cache

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.

Usage

Just drop the index.php file onto your server (preferably in its own directory), make sure permissions are set, and start making requests!

Required Parameters

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).

Basic example:

cache/?url=http://swapi.co/api/starships/9/?format=json&expire=3600

Optional Flags

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.

Example of multiple combined objects:

cache/?url=http://swapi.co/api/starships/9/?format=json,
http://swapi.co/api/people/4/?format=json
&expire=21600

Example of multiple merged objects:

  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.)

Bypass caching example:

cache/?url=http://swapi.co/api/films/1/?format=json&expire=3600&direct

Todo

  • 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().

About

Dead simple external resource caching (and concatenation).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages