特定快取的參照。
這個類別可讓您在快取中插入、擷取及移除項目。如果您想經常存取昂貴或速度緩慢的資源,這個做法特別實用。舉例來說,假設您在 example.com 有一個 RSS 動態消息,擷取時間需要 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。 |
get | Object | 傳回 JavaScript 物件,其中包含陣列中所有鍵的快取鍵/值組合。 |
put(key, value) | void | 將鍵/值組合新增至快取。 |
put(key, value, expirationInSeconds) | void | 將鍵/值組合新增至快取,並設定到期時間 (以秒為單位)。 |
put | void | 將一組鍵/值組合新增至快取。 |
put | void | 將一組鍵/值組合新增至快取,並設定到期時間 (以秒為單位)。 |
remove(key) | void | 使用指定鍵從快取中移除項目。 |
remove | void | 從快取中移除一組項目。 |
內容詳盡的說明文件
get(key)
取得指定鍵的快取值,如果找不到,則傳回 null。
// Gets the value from the cache for the key 'foo'. const value = CacheService.getScriptCache().get('foo');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
key | String | 要在快取中查找的鍵。 |
回攻員
String|null:快取值,如果找不到快取值,則為 null。
get All(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'}
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
keys | String[] | 要查詢的鍵。 |
回攻員
Object:JavaScript 物件,包含快取中所有鍵的鍵/值組合。
另請參閱
put(key, value)
將鍵/值組合新增至快取。
鍵的長度上限為 250 個半形字元。每個鍵可儲存的資料量上限為 100 KB。值會在 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');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
key | String | 用於儲存值的索引鍵。 |
value | String | 要快取的值。 |
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. CacheService.getScriptCache().put('foo', 'bar', 20);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
key | String | 用於儲存值的索引鍵。 |
value | String | 要快取的值。 |
expiration | Integer | 值保留在快取中的時間上限,以秒為單位。最短為 1 秒,最長為 21600 秒 (6 小時)。 |
put All(values)
將一組鍵/值組合新增至快取。
與重複呼叫「put」類似,但效率更高,因為它只會呼叫一次。每個鍵可儲存的資料量為 100 KB。這些值會在 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);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
values | Object | 包含字串鍵和值的 JavaScript 物件。 |
另請參閱
put All(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);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
values | Object | 包含字串鍵和值的 JavaScript 物件。 |
expiration | Integer | 值保留在快取中的最長時間 (以秒為單位)。允許的到期時間下限為 1 秒,上限為 21600 秒 (6 小時)。預設到期時間為 600 秒 (10 分鐘)。 |
另請參閱
remove(key)
使用指定鍵從快取中移除項目。
// Removes any cache entries for 'foo' CacheService.getUserCache().remove('foo');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
key | String | 要從快取中移除的鍵。 |
remove All(keys)
從快取中移除一組項目。
// Removes entries from the cache with keys 'foo' and 'x' CacheService.getDocumentCache().removeAll(['foo', 'x']);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
keys | String[] | 要移除的鍵陣列。 |