Caching fetched files in FlowBoost using FBCache

Using text files as lookup tables is one of FlowBoost’s awesome powers.

For example, here’s how one user fetches partner info — stored in a Marketo Design Studio file — keyed off a lead’s Email Domain field:

let partnerFile = "https://pages.example.com/rs/123-xor-456/images/partners.json"; 
let emailDomain = {{lead.Email Domain}};

FBHttp.fetch(partnerFile)
.then( (response) => response.json() )
.then( (partnerData) => 
  partnerData.find( (partner) => partner.domain == emailDomain ) 
)
.then( (partner) => partner )
.then( success )
.catch( failure )

Works fine, but that hosted .json file rarely changes, so it’s a major waste of resources to transfer it over the network on every FlowBoost call. You’re potentially adding seconds to the webhook execution time!

Enter FBCache, FlowBoost’s built-in temporary storage area.

The global FBCache object, unique to each API key, is a subclass of JavaScript Map. It takes an additional argument specifying the TTL (Time-To-Live) of each entry.

FBCache.set( "FirstName", "Sandy", "60s" ) stores the key "FirstName" with value "Sandy" for 60 seconds. (Use s, m, d for seconds, minutes, and days respectively.)

To make the above code more efficient, we can cache the contents of the .json file for 1 day, and check the cache first before fetching it:

let partnerFile = "https://pages.example.com/rs/123-xor-456/images/partners.json"; 
let emailDomain = {{lead.Email Domain}};

Promise.resolve(
  FBCache.get(partnerFile) ||
  FBHttp.fetch(partnerFile)
    .then( (response) => response.json() )
    .then( (partnerData) => FBCache.set(partnerFile, partnerData, "1d").get(partnerFile) )
)
.then( (partnerData) => 
  partnerData.find( (partner) => partner.domain == emailDomain ) 
)
.then( (partner) => partner )
.then( success )
.catch( failure )

You have more than one FBCache

FlowBoost scales to meet demand by adding more containers, and each container has a separate FBCache with its own entries (entries aren’t replicated).

Thus you may notice that some fetches still go over the network even if you thought you cached the file already. After an initial ramp-up period, though, each container’s cache will be “primed” with data.