Class Cache

快取

特定快取的參照。

這個類別可讓您插入、擷取及移除快取中的項目。當您想要頻繁存取昂貴或緩慢的資源時,這種做法特別實用。舉例來說,假設您在 example.com 有一個 RSS 動態消息,需要 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取得指定鍵的快取值;如果找不到,則傳回空值。
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)

取得指定鍵的快取值;如果找不到,則傳回空值。

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

參數

名稱類型說明
keyString要在快取中查詢的金鑰

回攻員

String:快取值;如果找不到,則傳回空值


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 個字元。每個鍵可儲存的資料量上限為 100 KB。該值會在 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 個字元。每個鍵可儲存的資料量上限為 100 KB。指定的到期時間只是建議,如果快取大量資料,則可在此之前移除快取資料。

快取項目的數量上限為 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 個字元。每個鍵可儲存的資料量上限為 100 KB。這些值會在 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 個字元。每個鍵可儲存的資料量上限為 100 KB。指定的到期時間僅供參考。如果快取資料很多,系統可能會在這個時間前移除快取資料。

快取項目的數量上限為 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[]要移除的鍵陣列