{"flow":{"id":39,"summary":"An example script on how to cache a script, for example a fetch call to an api. ","versions":[159],"created_by":"sindre svendby964","created_at":"2023-04-14T08:44:00.971Z","votes":0,"approved":false,"apps":["windmill"],"value":{"modules":[{"id":"a","value":{"lock":"","type":"rawscript","content":"import * as wmill from \"https://deno.land/x/windmill@v1.87.0/mod.ts\";\nimport {\n  format,\n  weekOfYear,\n} from \"https://deno.land/std@0.91.0/datetime/mod.ts\";\n\ntype periodType = \"Day\" | \"Month\" | \"Year\" | \"Hour\" | \"Week\";\n\nexport async function main(resourceCachePath: string, cachePeriod: string) {\n  let state;\n\n  const currentCachePeriod = format(\n    new Date(),\n    mapToFormatString(cachePeriod as periodType),\n  );\n\n  try {\n    state = await wmill.getResource(resourceCachePath);\n  } catch {\n    await wmill.setResource({}, resourceCachePath, \"state\");\n  }\n  console.log(state);\n  console.log(\"1state\");\n  if (!state) {\n    return {\n      cachePeriod: currentCachePeriod,\n      resourceCachePath: resourceCachePath,\n      cache: false,\n      value: undefined,\n    };\n  }\n\n  console.log(state.cachePeriod, currentCachePeriod);\n  if (state.cachePeriod == currentCachePeriod) {\n    return {\n      value: state.value,\n      cache: true,\n    };\n  }\n\n  return {\n    cache: false,\n    resourceCachePath: resourceCachePath,\n    cachePeriod: currentCachePeriod,\n    value: undefined,\n  };\n}\n\nfunction mapToFormatString(cachePeriod: periodType) {\n  switch (cachePeriod) {\n    case \"Day\":\n      return \"yyyy-MM-dd\";\n    case \"Hour\":\n      return \"yyyy-MM-dd:HH\";\n    case \"Month\":\n      return \"yyyy-MM\";\n    case \"Year\":\n      return \"yyyy\";\n    case \"Week\":\n      const week = weekOfYear(new Date());\n      return \"yyyy-week-\" + week;\n  }\n}\n","language":"deno","input_transforms":{"cachePeriod":{"expr":"flow_input.periodType","type":"javascript"},"resourceCachePath":{"expr":"`${flow_input.cachePath}`","type":"javascript"}}},"summary":"check If cache exist"},{"id":"b","value":{"type":"branchone","default":[{"id":"c","value":{"path":"hub/860/windmill/echo_argument","type":"script","input_transforms":{"arg":{"type":"static","value":"1"}}},"summary":"Echo argument (windmill)"},{"id":"d","value":{"type":"rawscript","content":"import * as wmill from \"https://deno.land/x/windmill@v1.87.0/mod.ts\";\n\nexport async function main(\n  resourceCachePath: string,\n  cacheValue: any,\n  cachePeriod: string,\n) {\n  const state = {\n    cachePeriod,\n    value: JSON.stringify(cacheValue),\n  };\n\n  await wmill.setResource(state, resourceCachePath);\n\n  return cacheValue;\n}\n","language":"deno","input_transforms":{"cacheValue":{"expr":"results.c","type":"javascript"},"cachePeriod":{"expr":"`${results.a.cachePeriod}`","type":"javascript"},"resourceCachePath":{"expr":"results.a.resourceCachePath","type":"javascript"}}},"summary":"update cache with the new result"}],"branches":[{"expr":"results.a.cache === true ","modules":[{"id":"e","value":{"type":"rawscript","content":"// import * as wmill from \"https://deno.land/x/windmill@v1.87.0/mod.ts\"\n\nexport async function main(value: any) {\n  return JSON.parse(value);\n}\n","language":"deno","input_transforms":{"value":{"expr":"results.a.value","type":"javascript"}}},"summary":"parse the state"}],"summary":""}]},"summary":""}]},"schema":{"type":"object","$schema":"https://json-schema.org/draft/2020-12/schema","required":["cachePeriod"],"properties":{"cachePath":{"type":"string","default":"","description":"the cache path needs to be in the format \"u/<your-user>/*\" or \"f/<valid-folder>/*\"\n"},"cachePeriod":{"type":"string","format":"","default":"Day","description":"One of \"Day\" | \"Month\" | \"Year\" | \"Hour\" | \"Week\".\n"}}},"description":"the flow consist of 3 parts.\n\n1. a script that checks the cache and return the value if true\n2. the script to execute if needed\n3. a script that save the value to the cache\n\nNote: \nThis do not handle concurrent request. The flow needs to get to step 3 to update the cache before a new request get past step 1.\n","recording":null,"vcreated_at":"2023-04-14T08:44:00.971Z","vcreated_by":"sindre svendby964","comments":[]}}