Google App Script Cache with persistence in SpreadSheet
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

gscache - Google App Script Cache Manager

gscache is a cache manager for Google Apps Script with persistence.

With this simple cache you can truly persist in GAS (Google Apps Script) cache items: in cache and in scriptDB SpreadSheet. Cache is volatile, so, with persistence in scriptDB SpreadSheet you can guarantee items are available always.

It splits big items into little ones and it reassembles them when you request for a key.

How to use

Create a new script file in your project and copy the code. Then, you can do:

  • gscache.put(key, value[, ttl=0])

    • ttl: in seconds, default cache ttl is unlimited
  • gscache.get(key)

  • gscache.remove(key)

  • gscache.removeAll()

Sample code

	function doGet(e) {
	  var c = gscache.get("publicflickr");

	    Logger.log("from live")
        var c = UrlFetchApp.fetch("").getContentText()
	    gscache.put("publicflickr", c, 3600)
	    Logger.log("from cache")
	  return ContentService.createTextOutput(JSON.stringify(c)).setMimeType(ContentService.MimeType.JSON);;

Spreadsheet vs ScriptDB

ScriptDB was a really cool feature in Google Apps Script, but it was deprecated in Nov 2014. Then, best alternative for pure Google Apps Script functionality was to use a SpreadSheet to persist data.

For each project that uses gscache a Spreadsheet will be generated in the root of your Drive. Each spreadsheet will have next name:

  • gscache.keyprefix (default is "gscache_") + "spreadsheet" + timestamp: "gscache_spreadsheet_123456789"

Once the spreadsheet is created you can move it to the folder of your choice.