GAS AI BuilderApps Script Assistant Sign in

Make slow reads fast with a cache

Apps Script recipes › APIs, cache and settings

Keeps the result of a slow read for a few minutes, so a page that opens often doesn't read the whole sheet each time.

File Code.gs

function cached_(key, seconds, compute) {
  const cache = CacheService.getScriptCache();
  const hit = cache.get(key);
  if (hit) return JSON.parse(hit);
  const value = compute();
  const text = JSON.stringify(value);
  if (text.length < 100000) cache.put(key, text, seconds);
  return value;
}

function getDashboard() {
  return cached_('dashboard', 300, () => readRows_('Orders'));
}

Good to know

Let the AI put it in your appGAS AI Builder knows every recipe here. Describe your app and it uses this code where it fits, in a Google Apps Script project in your own Drive.

Use it in a project

More in APIs, cache and settings

Call an API with retriesCalls an address, reads JSON, and tries again after a short wait when the service is busy (429 or 5xx).Keep settings and secrets out of the codeScript Properties hold API keys and settings; a setup function asks for them once.