将 Google API 结果无限期地存储在缓存中
Store Google API results in cache indefinitely
我正在尝试让下面的代码(Google Sheets 的自定义函数)在缓存中无限期地存储 API 查询的结果。
// 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 地图 API,该函数能够 return Google Sheet 中指定的两个位置之间的距离,并保存结果缓存 6 小时。如果先前已检索并缓存了新输入的位置,则将检索缓存的结果,否则将发送新的 API 查询。
根据 Class Cache
上的 Google's documentation,通过 put
调用,结果最多只能在内部缓存中存储 6 小时:
expirationInSeconds:
Integer:
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 IDs
(documentation) 的信息,这可能是 6 小时限制的可能解决方法。
来自 Places API Policies 文档:
Note that the place ID, used to uniquely identify a place, is exempt from the caching restriction. You can therefore store place ID values indefinitely. The place ID is returned in the place_id
field in Places API responses.
我发现的另一种可能的解决方法是在 Google Properties Service Documentation 中使用 Properties
,但我似乎无法将其实现到代码中。
如果有人愿意对此做出回应并帮助我,我将不胜感激。
您有 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 Sheets 的自定义函数)在缓存中无限期地存储 API 查询的结果。
// 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 地图 API,该函数能够 return Google Sheet 中指定的两个位置之间的距离,并保存结果缓存 6 小时。如果先前已检索并缓存了新输入的位置,则将检索缓存的结果,否则将发送新的 API 查询。
根据 Class Cache
上的 Google's documentation,通过 put
调用,结果最多只能在内部缓存中存储 6 小时:
expirationInSeconds:
Integer:
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 IDs
(documentation) 的信息,这可能是 6 小时限制的可能解决方法。
来自 Places API Policies 文档:
Note that the place ID, used to uniquely identify a place, is exempt from the caching restriction. You can therefore store place ID values indefinitely. The place ID is returned in the
place_id
field in Places API responses.
我发现的另一种可能的解决方法是在 Google Properties Service Documentation 中使用 Properties
,但我似乎无法将其实现到代码中。
如果有人愿意对此做出回应并帮助我,我将不胜感激。
您有 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);
};