2015年11月25日 星期三

Cache Service in Apps Script



Cache Service allows a script to temporarily cache results.

But puts values like Array into Cache, when you take it out will become a String.

Example :
#00#01#02
#10#11#12

Put array into cache when get will return a string.

#00,#01,#02,#10,#11,#12


So we can use Split string, return result to a array object.

#00#01#02#10#11#12

  1. var cache = CacheService.getUserCache();
  2. function putCache(key, array) {
  3.   cache.put(key, array);
  4. }
  5. function getCache(key) {
  6.   var result = cache.get(key).toString().split(",");
  7.   return result;
  8. }



Or we can use External APIs JSON,  is much easier to keep original Array structure


  1. var cache = CacheService.getUserCache();
  2. function putCache(key, array) {
  3.   var payload = JSON.stringify(array);
  4.   cache.put(key, payload );
  5. }
  6. function getCache(key) {
  7.   var payload = cache.get(key);
  8.   var result =  JSON.parse(payload);
  9.   return result;
  10. }

沒有留言:

張貼留言