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
Get a cache with CacheService.getScriptCache() for project-wide keys.
-
2
put(key, value, expirationInSeconds) stores a string with optional TTL up to 21600 seconds.
-
3
get(key) returns null on miss; then compute, put, and return fresh data.
-
4
remove(key) or removeAll() clears stale entries after sheet structure changes.
-
5
Document cache and user cache exist for narrower scopes when needed.
-
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.
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
Convert Google Sheets into a REST API
Related blog posts
Guides that explain CacheService in more depth.
How to Use Google Sheets as a Website Database
Did you know you can use Google Apps Script to turn a Google Sheet into a free, auto-syncing database for your website? This guide shows you exactly how to set it up. You’ll prepare your spreadsheet, write a few lines of code and publish your site. Don’t worry if you’re not a developer, we’ll walk you through it. Why Use Google Sheets as a Database? For small projects, Google Sheets offers many advantages over traditional databases: * Free hosting and storage * Easy to edit data without...
Read post →How to reference a different Google Spreadsheet in Apps Script?
Transferring Data Between Cells in Different Spreadsheets
Read post →Related terms
Apps Script web apps
A web app turns your script into a URL that runs doGet or doPost when someone visits or posts data. The page can read and write your spreadsheet behind the scenes, which is useful for simple forms, internal tools, and lightweight APIs without standing up a separate server.
Read guide →Google Sheets API
The Google Sheets API lets other programs read and update spreadsheets over HTTP using JSON. It is how dashboards, backends, and mobile apps sync data without opening the Google Sheets UI. Apps Script inside a file is simpler for sheet owners; the API fits systems built in Python, Node, or other stacks.
Read guide →Google Sheets automation
Automation means work happens without repeating the same clicks every day: imports update, emails send, rows move, and dashboards refresh. In Sheets, automation usually stacks built-in features, Apps Script, and sometimes the Sheets API or add-ons. Start with the lightest tool that still solves the job.
Read guide →Google Sheets workflows
A workflow is the path work takes through your sheet: intake, review, approval, done. Good workflows use consistent columns, statuses everyone understands, and just enough automation to move tasks forward without hiding steps from the team.
Read guide →Done reading about CacheService?
Membership unlocks 600+ 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.