Sunday, May 13, 2012

Cache 101 with Google Apps Script

If you have used the cache service long enough in your Google Apps Script, you probably have known you could not cache anything larger than 100KB.

If you are absolutely certain the values you are going to cache will not hit that magic number, you will be fine; but what if there is a chance your value could be 100 + 1? You can always check the size before you put, but that would be cumbersome and your code would be messy.

Here is a simple ready-to-use wrapper which hides the complexity and still performs equally well for size under 100KB.

It uses the first byte as an indicator: 'v' or 'k'. If the size is less than (100KB -1), it stores the data value; otherwise it chunks the data and stores the auto assigned keys as the value for the given key. The generated keys consist of the given key appended with a running number.

In addition, the wrapper automatically does the conversion between JSON object and string text, so you may store object instead.

To simplify the interface, you specify a creator function in place of a value. i.e.

cache.get(key, creator);

If the value exists in cache, it returns immediately; otherwise the creator function will be called.

Sample

1 comment:

Unknown said...

Thanks for the code sample. I've modified it for my purposes. It's in this Google Doc: https://docs.google.com/document/d/1c276AZNkicfX0PmyaZS2OSebrU84XmQHvHq9XZUMGWY/edit?usp=sharing