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
- One cache entry holds 100 KB at most; bigger results are simply not cached.
- Clear the key after a save (cache.remove) so people see their change right away.
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