| このクラスは項目をキャッシュに挿入、回収、削除することを提供します。とくに負荷のかかる低速なリソースに頻繁にアクセスしなければならないような時は
大変利用価値があります。たとえば自前のRSSフィードの回収に20秒もかかるような時、平均的な要求でアクセスできるようなスピードアップが見込めます。
|
|
function getRssFeed() {
var cache = CacheService.getPublicCache();
var cached = cache.get("rss-feed-contents");
if (cached != "") {
return cached;
}
var result = UrlFetchApp.fetch("http://example.com/my-slow-rss-feed.xml"); // takes 20 seconds to get
var contents = result.getContentText();
cache.put("rss-feed-contents", contents, 25000); // cache for 25 minutes
return contents;
}
|
|
上のスクリプトでは、項目が25分でキャッシュから期限切れになるまでの間、後続のアクセスが非常に高速になります。
(もし項目がキャッシュされていないと20秒ほど待たなくてなりません。)
|
| class Cache : Members |
| member | Type | 説明 |
|
get | String | 指定さられたキーに対するキャッシュされた値を返す;何も見つからない時はnullを返す |
|
getAll | Object | キャッシュに存在するすべてのキー/値のペアを含むjavascriptオブジェクトをキー配列のために返す |
|
put | | キー/値のペアをキャッシュに追加する |
|
put | | 最大存続時間(秒単位)でキー/値のペアをキャッシュに追加する |
|
putAll | | キー/値ペアのマップをキャッシュに追加する |
|
putAll | | 最大存続時間(秒単位)でキー/値ペアのマップをキャッシュに追加する |
|
remove | | 指定されたキーのキャッシュをエントリーから削除する |
|
removeAll | | キャッシュからエントリーのセットを削除する |
get(key)
-
指定されたキーに対するキャッシュされた値を返す;何も見つからない時はnullを返す
| Arguments : |
| name | Type | 説明 |
|
key | String |
キャッシュにおいて検索すべきキー |
| 戻り値 : |
| Type | 説明 |
|
String |
キャッシュされた値を表す文字列;見つからない時はnull
|
| サンプル : |
// Gets the value from the cache for the key 'foo'
var value = cache.get('foo');
|
getAll(keys)
-
キャッシュに存在するすべてのキー/値のペアを含むjavascriptオブジェクトをキー配列のために返す
参考;get
| Arguments : |
| name | Type | 説明 |
|
keys | String[ ] |
キャッシュにおいて検索すべきキーの配列 |
| 戻り値 : |
| Type | 説明 |
|
Object |
キャッシュに存在するすべてのキー/値のペアを含む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'}
|
put(key, value)
| Arguments : |
| name | Type | 説明 |
|
key | String |
キー/値のペアのためのキー |
|
value | String |
キャッシュされるべき値 |
| サンプル : |
// Puts the value 'bar' into the cache, keyed by 'foo'
cache.put('foo', 'bar');
|
put(key, value, expirationInSeconds)
-
最大存続時間(秒単位)でキー/値のペアをキャッシュに追加する
| Arguments : |
| name | Type | 説明 |
|
key | String |
キー/値のペアのためのキー |
|
value | String |
キャッシュされるべき値 |
|
expirationInSeconds | int |
キャッシュ内で値が存続できる最大時間(値は秒単位) |
| サンプル : |
// Puts the value 'bar' into the cache, keyed by 'foo', but will only be valid for 20 seconds
cache.put('foo', 'bar', 20);
|
putAll(values)
-
キー/値ペアのマップをキャッシュに追加する
put()の繰り返しに似ているが、すべての値の設定にmemcacheのサーバを1回だけ呼び出せば済むのでさらに効果的である
参考;put
| Arguments : |
| name | Type | 説明 |
|
values | Object |
キーと値を持つjavascriptオブジェクト |
| サンプル : |
// Puts a set of values into the cache: keys are 'foo', 'x', and 'key'.
cache.putAll({'foo': 'bar', 'x':'y', 'key': 'value'});
|
putAll(values, expirationInSeconds)
-
最大存続時間(秒単位)でキー/値ペアのマップをキャッシュに追加する
put()の繰り返しに似ているが、すべての値の設定にmemcacheのサーバを1回だけ呼び出せば済むのでさらに効果的である。
キャッシュエントリが有効である最大時間の設定ができる。
参考;put
| Arguments : |
| name | Type | 説明 |
|
values | Object |
キーと値を持つjavascriptオブジェクト |
|
expirationInSeconds | int |
キャッシュ内で値が存続できる最大時間(値は秒単位) |
| サンプル : |
// Puts a set of values into the cache: keys are 'foo', 'x', and 'key', valid for 20 seconds
cache.putAll({'foo': 'bar', 'x':'y', 'key': 'value'}, 20);
|
remove(key)
-
指定されたキーのキャッシュをエントリーから削除する
| Arguments : |
| name | Type | 説明 |
|
key | String |
キャッシュから削除すべきキー |
| サンプル : |
// Removes any cache entries for 'foo'
cache.remove('foo');
|
removeAll(keys)
| Arguments : |
| name | Type | 説明 |
|
keys | String[ ] |
キャッシュから削除すべきキーの配列 |
| サンプル : |
// Removes entries from the cache keyed by 'foo' and 'x'
cache.removeAll(['foo', 'x']);
|
|