Cache data for one day
@dylan
code:
data:privatelast updated:2 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 800,000+ developers using the Pipedream platform
steps.
trigger
HTTP API
Deploy to generate unique URL
This workflow runs on Pipedream's servers and is triggered by HTTP / Webhook requests.
steps.
check_cache_or_hit_api
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
}
36
const axios = require("axios")

const cache = $checkpoint || {}
const now = +new Date()

// Replace with whatever variable references your primary key
const { key } = steps.trigger.event.body

if (key in cache) {
  console.log(`Key ${key} is in cache`)
  const { expiry, data } = cache[key]
  if (expiry > now) {
    return data
  }
}

console.log(`Key ${key} is not in cache. Fetching data`)

// Since we don't have data cached for the key,
// or the data was expired, hit the API directly
const { data } = await axios({
  method: "GET",
  url: `https://swapi.dev/api/people/${key}`
})

$checkpoint = {
  ...cache,
  [key]: {
    data,
    expiry: now + (1000 * 60 * 60 * 24) // expiry one day in the future
  },
}

return data