SuperFetch is a proxy for UrlFetchApp with additional features such as built-in caching – see SuperFetch – a proxy enhancement to Apps Script UrlFetch for how it works and what it does.

This is another in my series on SuperFetch plugins.

Page Content hide
1 Caching – why bother?

Caching – why bother?

Apps Script provides a caching service that’s pretty fast – usually faster than accessing the source data. More importantly there aren’t rate limits or usage charges – so if you’re using an API with such charges or disruptive rate limits  (like the Twitter or Git API for example), you should keep API fetches to a minimum.

When does caching happen

When a Plugin performs a GET operation, SuperFetch first checks the cache and returns the last known value rather than going to the API.

If  SuperFetch does have to access the API, it also writes the data to cache under a key generated from the API request URL, a prefix you supply (normally the default one is fine), and potentially some header options.

SupeFetch never writes the result of POST and other write operations to cache.

noCache

All SuperFetch plugins have a noCache option. When set, the Plugin doesn’t write or read any cache data. In other words, noCache: true means always go to the API

During a noCache GET, it deletes any existing cache records for that key. The idea is that if anyone is doing a noCache operation it’s because they know the cache is out of date – so best to delete it now.

Pagination

By default, SuperFetch plugins deal with pagination automatically. They unpage and return the entire dataset in a single response. There is an option to deal with pagination manually if you really want to, but in principle, since these are server based queries, it’s likely that you’ll want all the data unpaged.

For automatic unpaging, the Plugin writes the entire unpaged dataset to cache – it’s not written a page at a time, because nextPageTokens are likely to become invalid between paging sessions.

Caching limitations

First of all the Apps Script Caching service has a size limit of 100k per entry – which is not a lot. Secondly you can’t easily know if data is stale, especially if you have multiple instances of the script running.

How Superfetch deals with these limitations

Under the hood SuperFetch uses this library Apps script caching with compression and enhanced size limitations,

Size limitations

It gets over the size limitation by

  • compressing all data before writing to caching and uncompressing to its original state on retrieval.
  • if the compressed data is still bigger than the Apps Script limit, it spreads it over multiple cache entries and keeps a record of the keys for all those entries

The cacher handles all this in the background, so you just put or get data of any size to the library.

Staleness

The biggest complication in caching is how to deal with staleness. Since cache keys are based on how to get data, you can’t know if any particular POST type operation is going to change the data associated with that key previously.

You can use noCache to invalidate a GET operation, but that’s rather a blunt instrument.

StaleKey

Eventually all SuperFetch Plugins will support 2 additional parameters at instantiation.

    // create a secrets manager instance
const smg = new Smg({
superFetch,
projectId: 'my-project',
// these are the default settings so typically, no need to set them
stale: true,
staleKey: 'smg'
})
stale & staleKey

Typically you won’t need to specify them (more on that later).

With stale enabled, a Plugin will store a unique value against the staleKey. It writes it to cache so any script using the same cache for this plugin will see it.

Here’s how:

  • When the plugin issues any POST type requests, it updates the value in this staleKey
  • When it generates key to recover data from cache, it includes this value in the key.

As a result it can never find any cache entries prior to the latest post, because the staleKey value will have changed.

Fine tuning with staleKey

Hopefully you will have realized that changing the staleKey value makes all cache entries for this Plugin unfindable, and in general that’s the safest approach. However,  you can create multiple instances of the plugin, each with a different staleKey of your choosing.

Like this, if you are sure that updating some data will not affect the results returned from some other query you can use different staleKeys for each type of data.

Which CacheService to use

It’s very important to choose the correct cache service when creating your SuperFetch instance. In most cases you’ll instantiate like this.

  const superFetch = new SuperFetch({
fetcherApp: UrlFetchApp,
tokenService: ScriptApp.getOAuthToken,
cacheService: CacheService.getUserCache()
})
usercache preferred

This is because different users potentially should get different results based on what their tokenService allows. If you use ScriptCache (or DocumentCache), you’re effectively sharing cache results between multiple users – meaning they may get data not intended for them. In some cases this could be correct for public or other shared data, but generally you’ll want to use UserCache to guarantee they will just see their own data.

Links

bmSuperFetch: 1B2scq2fYEcfoGyt9aXxUdoUPuLy-qbUC2_8lboUEdnNlzpGGWldoVYg2

IDE

GitHub

Related

file conversion

Convert any file with Apps Script

The Drive API offers a whole range of conversions between mimeTypes, but it's a little fiddly to figure out exactly ...
Superfetch plugin

Caching, property stores and pre-caching

I've written many times about various Apps Script caching techniques such as how to deal with size limits and use ...
document AI add-on

State management across CardService, HtmlService and Server side Add-ons

Motivation I've been working on a CardService Add-on lately which also uses HtmlService, and also runs quite a few things ...
Secret Manager

SuperFetch Plugin: Cloud Manager Secrets and Apps Script

Smg is a SuperFetch plugin to access the Google Cloud Secrets API. SuperFetch is a proxy for UrlFetchApp with additional ...
Superfetch plugin

SuperFetch caching: How does it work?

SuperFetch is a proxy for UrlFetchApp with additional features such as built-in caching – see SuperFetch – a proxy enhancement ...
superfetch tank drive

SuperFetch plugins: Tank events and appending

Tank and Drv are SuperFetch plugins to emulate streaming and use the Drive REST API with Apps Script. SuperFetch is ...
superfetch tank drive

SuperFetch Plugins: Apps Script streaming with Tank and Drive

Tank and Drv are SuperFetch plugins to emulate streaming and use the Drive REST API with Apps Script. SuperFetch is ...
superfetch tank apps script streaming

SuperFetch Tank Plugin: Streaming for Apps Script

Tank is a SuperFetch plugin to emulate streaming with Apps Script. SuperFetch is a proxy for UrlFetchApp with additional features ...
superfetch drive plugin logo

SuperFetch plugin – Google Drive client for Apps Script – Part 1

Drv is a SuperFetch plugin to access the Google Drive API. SuperFetch is a proxy for UrlFetchApp with additional features ...
Superfetch plugin twitter

SuperFetch – Twitter plugin for Apps Script – Get Follows, Mutes and blocks

Twt is a SuperFetch plugin to easily access to the Twitter v2 API. SuperFetch is a proxy for UrlFetchApp with ...
Superfetch plugin twitter

SuperFetch plugin – Twitter client for Apps Script – Counts

Twt is a SuperFetch plugin to easily access to the Twitter v2 API. SuperFetch is a proxy for UrlFetchApp with ...
goa twitter oauth2 apps script

OAuth2 and Twitter API – App only flow for Apps Script

I covered how to handle the somewhat more complex OAUTH2 authorization flow for the Twitter v2 API (OAuth 2.0 Authorization ...
Superfetch plugin twitter

SuperFetch plugin – Twitter client for Apps Script – Search and Get

Twt is a SuperFetch plugin to easily access to the Twitter v2 API. SuperFetch is a proxy for UrlFetchApp with ...
Goa Oauth2 for Apps Script

Apps Script Oauth2 library Goa: tips, tricks and hacks

Motivation Goa is a library to support OAuth2 for Apps Script connecting to a variety of services, using a variety ...
goa twitter oauth2 apps script

Apps Script Oauth2 – a Goa Library refresher

It's been a few years since I first created the Goa library. Initially it was mainly to provide OAuth2 authorization ...
SuperFetch

SuperFetch plugin – Firebase client for Apps Script

Frb is a SuperFetch plugin to easily access a Firebase Real time database. SuperFetch is a proxy for UrlFetchApp with ...
SuperFetch

SuperFetch plugin – iam – how to authenticate to Cloud Run from Apps Script

SuperFetch is a proxy for UrlFetchApp with additional features - see SuperFetch - a proxy enhancement to Apps Script UrlFetch ...
SuperFetch

SuperFetch – a proxy enhancement to Apps Script UrlFetch

I've written a few articles about JavaScript proxying on here, and I'm a big fan. I also use a lot ...
SuperFetch

Apps script caching with compression and enhanced size limitations

Motivation Caching is a great way to improve performance, avoid rate limit problems and even save money if you are ...

Simple but powerful Apps Script Unit Test library

Why unit testing? There are many test packages for Node (my favorite is ava) and there are also a few ...
up