Class Cache

缓存

对特定缓存的引用。

此类允许您在缓存中插入、检索和移除项。如果您需要经常访问昂贵或缓慢的资源,此功能会特别有用。例如,假设您在 example.com 上有一个 RSS Feed,获取该订阅源需要 20 秒,但您希望加快平均请求的访问速度。

function getRssFeed() {
  const cache = CacheService.getScriptCache();
  const cached = cache.get('rss-feed-contents');
  if (cached != null) {
    return cached;
  }
  const result = UrlFetchApp.fetch(
      'http://example.com/my-slow-rss-feed.xml');  // takes 20 seconds
  const contents = result.getContentText();
  cache.put('rss-feed-contents', contents, 1500);  // cache for 25 minutes
  return contents;
}

如果商品不在缓存中,您仍然需要等待 20 秒,但在 25 分钟内,后续调用会非常快,直到商品从缓存中过期。

方法

方法返回类型简介
get(key)String|null获取指定键的缓存值,如果未找到,则返回 null
getAll(keys)Object返回一个 JavaScript 对象,其中包含缓存中针对键数组找到的所有键值对。
put(key, value)void向缓存添加键值对。
put(key, value, expirationInSeconds)void向缓存添加键值对,并指定到期时间(以秒为单位)。
putAll(values)void向缓存添加一组键值对。
putAll(values, expirationInSeconds)void向缓存添加一组键值对,并指定过期时间(以秒为单位)。
remove(key)void使用给定键从缓存中移除条目。
removeAll(keys)void从缓存中移除一组条目。

详细文档

get(key)

获取指定键的缓存值,如果未找到,则返回 null

// Gets the value from the cache for the key 'foo'.
const value = CacheService.getScriptCache().get('foo');

参数

名称类型说明
keyString要在缓存中查找的键。

返回

String|null - 缓存的值;如果未找到,则为 null


getAll(keys)

返回一个 JavaScript 对象,其中包含缓存中针对键数组找到的所有键值对。

// Gets a set of values from the cache
const values = CacheService.getDocumentCache().getAll(['foo', 'x', 'missing']);
// If there were values in the cache for 'foo' and 'x' but not 'missing', then
// 'values' is: {'foo': 'somevalue', 'x': 'othervalue'}

参数

名称类型说明
keysString[]要查找的键。

返回

Object - 一个 JavaScript 对象,包含缓存中找到的所有键的键值对。

另请参阅


put(key, value)

向缓存添加键值对。

键的长度上限为 250 个字符。每个键可存储的数据量上限为 100KB。相应值会在 600 秒(10 分钟)后从缓存中过期。

缓存内容的上限为 1,000 项。如果写入的项超过 1,000 个,缓存会存储距离过期时间最远的 900 个项。此限制可能会发生变化。

const cache = CacheService.getScriptCache();
// Puts the value 'bar' into the cache using the key 'foo'
cache.put('foo', 'bar');

参数

名称类型说明
keyString用于存储值的键。
valueString要缓存的值。

put(key, value, expirationInSeconds)

向缓存添加键值对,并指定到期时间(以秒为单位)。

键的长度上限为 250 个字符。每个键可存储的数据量上限为 100KB。指定的过期时间仅为建议值;如果缓存了大量数据,缓存的数据可能会在此时间之前被移除。

缓存内容的上限为 1,000 项。如果写入的项超过 1,000 个,缓存会存储距离过期时间最远的 900 个项。此限制可能会发生变化。

// Puts the value 'bar' into the cache using the key 'foo', but only for the
// next 20 seconds.
CacheService.getScriptCache().put('foo', 'bar', 20);

参数

名称类型说明
keyString用于存储值的键。
valueString要缓存的值。
expirationInSecondsInteger值在缓存中保留的最长时间(以秒为单位)。最短为 1 秒,最长为 21600 秒(6 小时)。

putAll(values)

向缓存添加一组键值对。

与重复调用“put”类似,但效率更高,因为它只对每个键可存储的数据量进行一次调用,即 100KB。这些值会在 600 秒(10 分钟)后从缓存中过期。

缓存内容的上限为 1,000 项。如果写入的项超过 1,000 个,缓存会存储距离过期时间最远的 900 个项。此限制可能会发生变化。

// Puts a set of values into the cache with the keys 'foo', 'x', and 'key'.
const values = {
  foo: 'bar',
  x: 'y',
  key: 'value',
};
CacheService.getUserCache().putAll(values);

参数

名称类型说明
valuesObject一个包含字符串键和值的 JavaScript 对象。

另请参阅


putAll(values, expirationInSeconds)

向缓存添加一组键值对,并指定过期时间(以秒为单位)。

与重复调用“put”类似,但效率更高,因为它只需调用一次 memcache 服务器即可设置所有值。键的长度上限为 250 个字符。每个键可存储的数据量上限为 100 KB。指定的过期时间仅为建议值;如果缓存了大量数据,缓存的数据可能会在此时间之前被移除。

缓存内容的上限为 1,000 项。如果写入的项超过 1,000 个,缓存会存储距离过期时间最远的 900 个项。此限制可能会发生变化。

// Puts a set of values into the cache with the keys 'foo', 'x', and 'key'.
const values = {
  foo: 'bar',
  x: 'y',
  key: 'value',
};
CacheService.getUserCache().putAll(values, 20);

参数

名称类型说明
valuesObject一个包含字符串键和值的 JavaScript 对象。
expirationInSecondsInteger值在缓存中保留的最长时间(以秒为单位)。允许的最小到期时间为 1 秒,允许的最大到期时间为 21600 秒(6 小时)。默认过期时间为 600 秒(10 分钟)。

另请参阅


remove(key)

使用给定键从缓存中移除条目。

// Removes any cache entries for 'foo'
CacheService.getUserCache().remove('foo');

参数

名称类型说明
keyString要从缓存中移除的键。

removeAll(keys)

从缓存中移除一组条目。

// Removes entries from the cache with keys 'foo' and 'x'
CacheService.getDocumentCache().removeAll(['foo', 'x']);

参数

名称类型说明
keysString[]要移除的键的数组。