将Google API结果无限期存储在缓存中 [英] Store Google API results in cache indefinitely

查看:60
本文介绍了将Google API结果无限期存储在缓存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让下面的代码(Google表格的自定义功能)将API查询的结果无限期地存储在缓存中.

I am trying to have the code (a custom function for Google Sheets) below store the results of the API queries in a cache indefinitely.

// The cache key for "New York" and "new york  " should be same
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 getCache = (key) => {
  return CacheService.getDocumentCache().get(md5(key));
};

// Store the results for 6 hours
const setCache = (key, value) => {
  const expirationInSeconds = 6 * 60 * 60;
  CacheService.getDocumentCache().put(md5(key), value, expirationInSeconds);
};

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

  const key = ['distance', origin, destination, mode].join(',');
  // Is result in the internal cache?
  const value = getCache(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
  setCache(key, distance);
  return distance;
};

使用Google Maps API,该函数可以返回Google表格中指定的两个位置之间的距离,并将结果保存在缓存中6个小时.如果先前已检索并缓存了新输入的位置,则将检索缓存的结果,否则,将发送新的API查询.

Using the Google Maps API, the function is able to return the distances between two locations specified in a Google Sheet, and save the results in a cache for 6 hours. If the newly entered locations have previously been retrieved and cached, the cached result will be retrieved, otherwise, a new API query is sent.

根据 Google的文档 Class Cache 上,通过 put 调用,结果只能在内部缓存中存储长达6个小时:

According to Google's documentation on Class Cache, the results can only be stored in the internal cache for up to 6 hours with the put call:

expirationInSeconds:

expirationInSeconds:

整数:

该值保留在缓存中的最长时间(以秒为单位).最小为1秒,最大为21600秒(6小时).

the maximum time the value remains in the cache, in seconds. The minimum is 1 second and the maximum is 21600 seconds (6 hours).

尽管如此,我还是了解了 Place ID (

Although, I read about Place IDs (documentation) that could be a possible workaround for the 6 hour limit.

Places API政策文档中:

请注意,用于唯一标识位置的位置ID免于缓存限制.因此,您可以无限期地存储场所ID值.在Places API响应的 place_id 字段中返回位置ID.

我发现的另一种可能的解决方法是在

Another possible workaround that I have found is using Properties in the Google Properties Service Documentation, but I can not seem to implement it into the code.

如果有人愿意回答并帮助我,我将不胜感激.

I would highly appreciate it if anyone would be kind enough to respond and help me with this.

推荐答案

您有2个选项:

  • 使用属性"无限期存储数据.
  • 继续使用缓存并设置一个触发每5小时(在缓存过期之前)恢复缓存其他6小时的价值.

https://developers.google.com/apps-script/reference/properties

要使用属性,请为此更改getCache和setCache函数:

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

这篇关于将Google API结果无限期存储在缓存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆