What is CacheService in Google Sheets Apps Script?

CacheService stores short-lived strings in memory-like cache scoped to the script. Scripts use it to avoid rereading huge ranges or calling slow APIs on every web app hit. Entries expire automatically after a time-to-live you choose, up to six hours per key.

When to use it

Use CacheService when repeated reads are expensive: cache a JSON lookup table for doGet, store API responses for five minutes, or hold computed summary stats between trigger runs in the same busy hour.

When to skip it

Skip CacheService when data must always be real time, or when values are too large. Cache values max around 100 KB per key and total cache space is limited.

How it works

  1. 1

    Get a cache with CacheService.getScriptCache() for project-wide keys.

  2. 2

    put(key, value, expirationInSeconds) stores a string with optional TTL up to 21600 seconds.

  3. 3

    get(key) returns null on miss; then compute, put, and return fresh data.

  4. 4

    remove(key) or removeAll() clears stale entries after sheet structure changes.

  5. 5

    Document cache and user cache exist for narrower scopes when needed.

  6. 6

    Serialize objects with JSON.stringify before put and JSON.parse after get.

Examples in Google Sheets

Web app lookup cache

doGet checks cache for product id. On miss, read the Products sheet, put JSON in cache for 300 seconds, then return the row.

Rate API relief

UrlFetchApp result is cached ten minutes so fifty sidebar searches do not hammer the same external endpoint.

Dashboard snapshot

A trigger refreshes KPI JSON in cache hourly while the sidebar reads cache for instant display.

Build this without starting from a blank cell

Use a Better Sheets tool for CacheService, then watch a walkthrough when you want the full pattern.

Better Sheets resources

Common mistakes

  • Caching huge sheet dumps that exceed per-key size limits.
  • Using long TTL when users expect immediate updates after sheet edits.
  • Forgetting cache is best-effort; entries may evict early under load.
  • Storing secrets in cache keys or values visible in debug logs.
  • Same cache key for different users when data should be per-user; use User cache or key suffixes.

Frequently asked questions

How long can cache entries last?
Up to 21600 seconds (six hours) per put call. Shorter TTL keeps data fresher.
What is the max value size?
Google documents roughly 100 KB per key. Split or summarize larger payloads.
Is CacheService shared across users?
Script cache is shared for the project. User cache is per user. Document cache is per spreadsheet file.
Does cache survive script edits?
Entries persist until TTL expires or you remove them. Deployments do not clear cache automatically.
Can I cache getValues results?
Yes, if JSON.stringify of the array fits size limits and slight staleness is acceptable.
What happens on cache miss?
get returns null. Your code should recompute and put the new value.
Is CacheService the same as PropertiesService?
No. Properties persist without TTL until deleted. Cache is temporary and size-limited for speed.
Can triggers use cache?
Yes. Time-driven and web app handlers use CacheService like any other function.

Related Tutorials

Watch how CacheService works

Browse more tutorials
Convert Google Sheets into a REST API

Convert Google Sheets into a REST API

How to use Google Sheets as a database by turning it into a REST API wit...

Related blog posts

Guides that explain CacheService in more depth.

Browse the blog

Related glossary terms

Done reading about CacheService?

Membership unlocks 636+ tutorials, unlimited generators, and every template. Practical lessons. Zero fluff.

Need this once

Jump to a free tool or a single tutorial for this topic.

Learning Sheets for real

Unlock the full library, generators, and templates with membership.