Origami comics

Cache slow resources

Are some of your site’s resources slow to retrieve or generate? You can cache them!

cityMaps.ori
//
// This little Origami program generates a folder of street map images for
// cities in a data file. This uses the nice Mapbox Static Images API, but
// making network calls can be a little slow.
//
Tree.mapExtension(
  cities.yaml,
  "→.png",
  ({ lat, long }) => fetch(
    `https://api.mapbox.com/styles/v1/mapbox/streets-v12/static/pin-s+ff0000(${long},${lat})/${long},${lat},9/400x400@2x?access_token=${ token.txt }`
  )?.arrayBuffer()  
)

In Origami, a cache is a general operation that combines a slow tree of resources and a fast one.

site.ori
{
  // The maps route of our site has a cache to speed local development
  maps/: Tree.cache(
    cityMaps.ori, // slow tree based on network API calls
    files:mapImages // fast tree based on local files
  )
}

If you ask for a resource the fast tree doesn’t have yet, the cache gets the resource from the slow tree and saves it for next time.

$ ls mapImages
bogotá.png      sãoPaulo.png
$ ori site.ori/maps/lima.png > /dev/null
$ ls mapImages
bogotá.png      lima.png        sãoPaulo.png
$ 

Asking for a new map adds it to local files.

Making it more pleasant to use generated resources can open up more creative possibilities for your site!

http://localhost:5000/maps/lima.png

Read more: Origami's Tree.cache builtin

  Comic index