Class Cache

缓存

对特定缓存的引用。

该类允许您在缓存中插入、检索和移除项。当您需要频繁访问昂贵或缓慢的资源时,这种方法特别有用。例如,假设您在 example.com 上有一个 RSS Feed,其获取时间为 20 秒,但您希望提高平均请求的访问速度。

function getRssFeed() {
  var cache = CacheService.getScriptCache();
  var cached = cache.get("rss-feed-contents");
  if (cached != null) {
    return cached;
  }
  var result = UrlFetchApp.fetch("http://example.com/my-slow-rss-feed.xml"); // takes 20 seconds
  var contents = result.getContentText();
  cache.put("rss-feed-contents", contents, 1500); // cache for 25 minutes
  return contents;
}
如果该项不在缓存中,您仍然需要等待 20 秒,但后续调用将会非常快,直到该项在 25 分钟内从缓存中过期。

方法

方法返回类型简介
get(key)String获取给定键的缓存值;如果未找到,则返回 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'.
var value = cache.get('foo');

参数

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

弃踢回攻

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


getAll(keys)

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

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

参数

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

弃踢回攻

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

另请参阅


put(key, value)

向缓存添加键值对。

键的长度上限为 250 个字符。每个密钥可以存储的最大数据量为 100KB。该值将在 600 秒(10 分钟)后从缓存中过期。

缓存内容的数量上限为 1,000。如果写入的内容超过 1,000 个,则缓存将存储距离到期最远的 900 个内容。此上限可能会更改。

// 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.
cache.put('foo', 'bar', 20);

参数

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

putAll(values)

向缓存添加一组键值对。

与重复调用“put”类似,但更高效,因为它仅调用 Memcache 服务器来设置所有值。键的长度上限为 250 个字符。每个密钥可以存储的最大数据量为 100KB。这些值将在缓存中保留 600 秒(10 分钟)后过期。

缓存内容的数量上限为 1,000。如果写入的内容超过 1,000 个,则缓存将存储距离到期最远的 900 个内容。此上限可能会更改。

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

参数

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

另请参阅


putAll(values, expirationInSeconds)

向缓存添加一组键值对,以及到期时间(以秒为单位)。

与重复调用“put”类似,但更高效,因为它仅调用 Memcache 服务器来设置所有值。键的长度上限为 250 个字符。每个密钥可以存储的最大数据量为 100KB。指定的到期时间只是建议时间;如果缓存大量数据,系统可能会在此时间之前移除缓存的数据。

缓存内容的数量上限为 1,000。如果写入的内容超过 1,000 个,则缓存将存储距离到期最远的 900 个内容。此上限可能会更改。

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

参数

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

另请参阅


remove(key)

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

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

参数

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

removeAll(keys)

从缓存中移除一组条目。

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

参数

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