Skip to content

Instantly share code, notes, and snippets.

@pilbot
Created January 6, 2016 11:47
Show Gist options
  • Save pilbot/9d0567ef1daf556449fb to your computer and use it in GitHub Desktop.
Save pilbot/9d0567ef1daf556449fb to your computer and use it in GitHub Desktop.
Using the Google Apps Script Cache Service for objects above 100Kb
function ChunkyCache(cache, chunkSize){
return {
put: function (key, value, timeout) {
var json = JSON.stringify(value);
var cSize = Math.floor(chunkSize / 2);
var chunks = [];
var index = 0;
while (index < json.length){
cKey = key + "_" + index;
chunks.push(cKey);
cache.put(cKey, json.substr(index, cSize), timeout+5);
index += cSize;
}
var superBlk = {
chunkSize: chunkSize,
chunks: chunks,
length: json.length
};
cache.put(key, JSON.stringify(superBlk), timeout);
},
get: function (key) {
var superBlkCache = cache.get(key);
if (superBlkCache != null) {
var superBlk = JSON.parse(superBlkCache);
chunks = superBlk.chunks.map(function (cKey){
return cache.get(cKey);
});
if (chunks.every(function (c) { return c != null; })){
return JSON.parse(chunks.join(''));
}
}
}
};
};
function testGetCacheFrom(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
var data = sheet.getDataRange().getValues();
var chunky = ChunkyCache(CacheService.getDocumentCache(), 1024*90);
chunky.put('Data', data, 120);
var check = chunky.get('Data');
var sheetPut = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Out');
for (c in check) {
sheetPut.appendRow(check[c]);
}
}
@RealSlimMahdi
Copy link

RealSlimMahdi commented May 30, 2022

Hi, thank you very much for this piece of code, me too I wish GAS would allow bigger caching space:

I had to add a remove method for my project and linted the code, so I am sharing here in case someone needs it someday:

function ChunkyCache(cache, chunkSize) {
  return {
    put: function put(key, value, timeout) {
      const json = JSON.stringify(value);
      const cSize = Math.floor(chunkSize / 2);
      const chunks = [];
      let index = 0;
      while (index < json.length) {
        const cKey = `${key}_${index}`;
        chunks.push(cKey);
        cache.put(cKey, json.substr(index, cSize), timeout + 5);
        index += cSize;
      }

      const superBlk = {
        chunkSize,
        chunks,
        length: json.length,
      };
      cache.put(key, JSON.stringify(superBlk), timeout);
    },
    get: function get(key) {
      const superBlkCache = cache.get(key);
      if (superBlkCache != null) {
        const superBlk = JSON.parse(superBlkCache);
        const chunks = superBlk.chunks.map(cKey => {
          cache.get(cKey)
        });
        if (chunks.every(c => c != null)) {
          return JSON.parse(chunks.join(''));
        }
      }
      return null;
    },
    remove: function remove(key) {
      const superBlkCache = cache.get(key);
      if (superBlkCache != null) {
        const superBlk = JSON.parse(superBlkCache);
        return cache.removeAll([...superBlk.chunks]);
      }
      return null;
    }
  };
}

@oshliaer
Copy link

@RealSlimMahdi
Copy link

RealSlimMahdi commented May 30, 2022

@oshliaer
Copy link

@RealSlimMahdi , look at the remove method. They are identical =)

@RealSlimMahdi
Copy link

Yes, I have seen 🤝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment