1

I have been trying to use caching in my code, but I keep getting the following error:

ReferenceError: cache is not defined.

I've tried looking in the documentation and on the internet as to what the error means, but I haven't found anything.

1

2 Answers 2

2

I played around with the cache service a bit. Perhaps this will help.

function getMyData() {
  let cs=CacheService.getScriptCache();//select which cache
  let dt=JSON.parse(cs.get('mydata'));//parsing data back into object
  if(!dt) {//if no data in cache
    const ss=SpreadsheetApp.openById(gobj.globals.targetid);
    const sh=ss.getSheetByName(gobj.globals.targetsh);
    const vs=sh.getDataRange().getValues();
    cs.put('mydata',JSON.stringify(vs));//put data into cache
    dt=vs;//get data
    ss.toast('accessed cache');//just to let me know I accessed cache
  }
  return dt;
}

function displayMyData() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('Sheet1');
  sh.clearContents();
  let dt=getMyData();//gets data from original source or cache if it's availiable
  sh.getRange(1,1,dt.length,dt[0].length).setValues(dt);
}

function flushcache() {
  let cs=CacheService.getScriptCache();
  cs.removeAll(['mydata']);
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('Sheet1');
  sh.clearContents();
  ss.toast('flushed');
}

gobj.globals are just global variable I use

Sign up to request clarification or add additional context in comments.

Comments

1

Try implementing the code below:


const md5 = (key = '') => {
  const code = key.toLowerCase().replace(/\s/g, '');
  return Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, key)
    .map((char) => (char + 256).toString(16).slice(-2))
    .join('');
};

const getProperty = (key) => {
  var scriptProperties = PropertiesService.getScriptProperties();
  return scriptProperties.getProperty(md5(key));
};

const setProperty = (key, value) => {
  var scriptProperties = PropertiesService.getScriptProperties();
  scriptProperties.setProperty(md5(key), value);
};

Once this is implemented into your code, you can use the getProperties to retrieve any of the previously stored results. Like in the example below:

const md5 = (key = '') => {
  const code = key.toLowerCase().replace(/\s/g, '');
  return Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, key)
    .map((char) => (char + 256).toString(16).slice(-2))
    .join('');
};

const getProperty = (key) => {
  var scriptProperties = PropertiesService.getScriptProperties();
  return scriptProperties.getProperty(md5(key));
};

const setProperty = (key, value) => {
  var scriptProperties = PropertiesService.getScriptProperties();
  scriptProperties.setProperty(md5(key), value);
};

const GOOGLEMAPS_DISTANCE = (origin, destination, mode = 'driving') => {

  const key = ['distance', origin, destination, mode].join(',');
  // Is result in the internal cache?
  const value = getProperty(key);
  // If yes, serve the cached result
  if (value !== null) return value;
  const { routes: [data] = [] } = Maps.newDirectionFinder()
    .setOrigin(origin)
    .setDestination(destination)
    .setMode(mode)
    .getDirections();
  if (!data) {
    GOOGLEMAPS_DISTANCE;
  }
  const { legs: [{ distance: { text: distance } } = {}] = [] } = data;
  // Store the result in internal cache for future
  setProperty(key, distance);
  return distance;
};

In this example, Properties are used instead of Cache because it lets you store data indefinitely, while Cache is limited to 6 hours. Please see this thread for further information on the examples.

Documentation for Properties

Documentation for Cache

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.