Let’s say you have a large array of objects you want to write to cache. You can write it like this
cache.put (cacheKey , Utilities.base64Encode(Utilities.zip ([Utilities.newBlob(JSON.stringify(yourObject))]).getBytes()));
and read it back again like this
var cached = cache.get (cacheKey); if (cached) { var yourObject = JSON.parse( Utilities.unzip(Utilities.newBlob(Utilities.base64Decode(cached),'application/zip'))[0].getDataAsString()); }
JSON is pretty compressible, so you’re likely to get a 60-70% reduction in size.
You can of course play this trick with the properties service too, which has a much lower property values size than cache service.
Library version
Since this is handful, it is implemented in my cUseful library.
Here’s the key for the cUseful library, and it’s also on github, or below.
Mcbr-v4SsYKJP7JMohttAZyz3TLx7pV4j
And the above code could be rewritten as follows using crush and uncrush.
cache.put (cacheKey , cUseful.Utils.crush (JSON.stringify(yourObject))); var cached = cache.get (cacheKey); if (cached) { var yourObject = JSON.parse(cUseful.Utils.uncrush (cached)); }
that’s all folks…