Wednesday, 5 September, 2018 UTC


Summary

Fly's caching API reads from and writes to regional, in memory cache servers. The most recent Fly release includes two big cache features — so you can solve one of the two hard problems in computer science:
  1. Cache invalidation
  2. Naming things
  3. Off by one errors
Tag cache entries (#114)
Cached data is typically related with shared dependencies, if you're serving a bunch of image transformations, they might all depend on one "master" image.
Cache tags are a mechanism for "tagging" cached items with dependencies — in our image example we might want to specify that a thumbnail depends on a master image:
import cache from "@fly/cache";

const url = "http://example.com/big.jpg"

const key = url + ":thumb"
let thumb = await cache.get(key)
if(!thumb){
  const master = await fetchImage(url)
  thumb = await master.resize(240).toBuffer()
  
  // cache thumbnail, with master URL as tag
  await cache.set(key, thumb, { tags: url })
}
... and when the master changes, we can purge all the transformed versions in one call:
import cache from "@fly/cache"

const url = "http://example.com/big.jpg"

await cache.purgeTag(url)
Global delete and purge (#145)
Fly's cache is regional, which is ideal for Edge Applications since most global caches vary tremendously between regions. But there can be shared content scattered around the world, and it's useful to be able to delete it all in one go.
The new cache.global API lets developers propagate cache notifications around the world. It turns the Fly cache into something like a distributed, eventually consistent global cache. Use cache.global.del(key) to eventually delete all named keys in all regions. Or cache.global.purgeTag(tag) to eventually purge all cached items with a given tag.
In practice, these notifications only takes a few hundred milliseconds to propagate, but when the internet is being obstinate it can take longer.
Bonus: cache Response objects (#132)
We've also added a quick little helper for caching/retrieving HTTP response objects. Check cache, fetch, write Response to cache is very common in Edge Applications. The responseCache efficiently encodes headers + body for cache storage:
import { responseCache } from "@fly/cache"

const url = "http://example.com/shenanigans/")
let resp = await responseCache.get(url)

if(!resp){
  resp = await fetch(url)
  await responseCache.set(url, resp, { ttl: 3600 })
}
Oh, and it also supports tags. So you can cache/delete big groups of related HTTP responses all in one go.
Give it a try
npm install -g @fly/fly will get you running locally. Then have a look at the example apps in our source repository for starting points.