【问题标题】:Using cache in Google Apps Script在 Google Apps 脚本中使用缓存
【发布时间】:2021-04-13 15:14:09
【问题描述】:

我一直在尝试在我的代码中使用缓存,但我不断收到以下错误:

ReferenceError: cache is not defined.

我已经尝试在文档和互联网上查看错误的含义,但我没有找到任何东西。

【问题讨论】:

标签: google-apps-script


【解决方案1】:

我玩了一下缓存服务。也许这会有所帮助。

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 只是我使用的全局变量

【讨论】:

    【解决方案2】:

    尝试实现以下代码:

    
    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);
    };
    
    

    一旦在您的代码中实现这一点,您就可以使用getProperties 检索任何以前存储的结果。如下例所示:

    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;
    };
    

    在此示例中,使用 Properties 代替 Cache,因为它允许您无限期地存储数据,而 Cache 被限制为 6 小时。有关示例的更多信息,请参阅this 线程。

    DocumentationProperties

    DocumentationCache

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      相关资源
      最近更新 更多