第1章 $fh.cache

$fh.cache(options, callback);

クラウドアプリのキャッシュ層にある値を保存、読み込み、削除します。

デフォルトでは、キャッシュストアのメモリー上限は 1GB です。この上限に達すると、LRU (最も長く使われていない) アルゴリズムを使用してデータの削除を開始します。

キャッシュに値を保存する際に "expire" オプションが指定されていない場合は、その値はメモリー上限に達するまでキャッシュにとどまります。その後は、どの値を削除するかは LRU アルゴリズムで判断されます。

1.1. キャッシュオプションの管理

キャッシュのメモリー上限は Studio で管理が可能で、キャッシュストアのフラッシュもできます。これら 2 つのオプションは、リソース > [環境] > キャッシュ で設定できます。

注記

キャッシュの最大サイズの設定は、環境内の全クラウドアプリに影響します。

キャッシュの管理

1.2. 例

値をキャッシュに保存

var options = {
  "act": "save",
  "key": "foo", // The key associated with the object
  "value": "bar", // The value to be cached, must be serializable
  "expire": 60 // Expiry time in seconds. Optional
};
$fh.cache(options, function (err, res) {
  if (err) return console.error(err.toString());

  // res is the original cached object
  console.log(res.toString());
});

キャッシュから値を読み込み

var options = {
  "act": "load",
  "key": "foo" // key to look for in cache
};
$fh.cache(options, function (err, res) {
  if (err) return console.error(err.toString());

  // res is the original cached object
  console.log(res.toString());
});

キャッシュから値を削除

var options = {
  "act": "remove",
  "key": "foo" // key to look for in cache
};
$fh.cache(options, function (err, res) {
  if (err) return console.error(err.toString());

  // res is the removed cached object
  console.log(res.toString());
});