AmazonSimpleAdmin にオブジェクトキャッシュを適用してみました。
AsaCore.php の _parseTpl() メソッドに、以下のように wp_cache_get() と wp_cache_set() を適用すればよろし。
protected function _parseTpl ($asin, $tpl)
{
$tracking_id = '';
if (!empty($this->amazon_tracking_id)) {
// use the user's tracking id
$tracking_id = $this->amazon_tracking_id;
} else {
// otherwise use mine (for all my good programming work
if (empty($this->amazon_country_code)) {
$tracking_id = $this->my_tacking_id['US'];
} else {
$tracking_id =
$this->my_tacking_id[$this->amazon_country_code];
}
}
$search = $this->_getTplPlaceholders(true);
$replace = wp_cache_get($asin,'AmazonSimpleAdmin');
if($replace){
// get item data from cache
return preg_replace($search, $replace, $tpl);
}
// get the item data from amazon
$item = $this->_getItem($asin);
if ($item === null) return '';
$lowestOfferPrice = null;
$amazonPrice = $item->Offers->Offers[0]->Price;
if ($item->Offers->LowestUsedPrice && $item->Offers->LowestNewPrice) {
$lowestOfferPrice =
($item->Offers->LowestUsedPrice < $item->Offers->LowestNewPrice) ?
$item->Offers->LowestUsedPrice : $item->Offers->LowestNewPrice;
$lowestOfferCurrency =
($item->Offers->LowestUsedPrice < $item->Offers->LowestNewPrice) ?
$item->Offers->LowestUsedPriceCurrency :
$item->Offers->LowestNewPriceCurrency;
} else if ($item->Offers->LowestNewPrice) {
$lowestOfferPrice = $item->Offers->LowestNewPrice;
$lowestOfferCurrency = $item->Offers->LowestNewPriceCurrency;
} else if ($item->Offers->LowestUsedPrice) {
$lowestOfferPrice = $item->Offers->LowestUsedPrice;
$lowestOfferCurrency = $item->Offers->LowestUsedPriceCurrency;
}
$lowestOfferPrice = $this->_formatPrice($lowestOfferPrice);
$amazonPrice = $this->_formatPrice($amazonPrice);
$totalOffers = $item->Offers->TotalNew + $item->Offers->TotalUsed +
$item->Offers->TotalCollectible + $item->Offers->TotalRefurbished;
if (empty($this->amazon_country_code)) {
$amazon_url = sprintf($this->amazon_url['US'],
$item->ASIN, $tracking_id);
} else {
$amazon_url = sprintf($this->amazon_url[$this->amazon_country_code],
$item->ASIN, $tracking_id);
}
$platform = $item->Platform;
if (is_array($platform)) {
$platform = implode(', ', $platform);
}
$author = $item->Author;
if (is_array($author)) {
$author = implode(', ', $author);
}
$artist = $item->Artist;
if (is_array($artist)) {
$artist = implode(', ', $artist);
}
$replace = array(
($item->SmallImage != null) ? $item->SmallImage->Url->getUri() :
get_bloginfo('wpurl') . $this->plugin_dir . '/img/no_image.gif',
($item->SmallImage != null) ? $item->SmallImage->Width : 60,
($item->SmallImage != null) ? $item->SmallImage->Height : 60,
($item->MediumImage != null) ? $item->MediumImage->Url->getUri() :
get_bloginfo('wpurl') . $this->plugin_dir . '/img/no_image.gif',
($item->MediumImage != null) ? $item->MediumImage->Width : 60,
($item->MediumImage != null) ? $item->MediumImage->Height : 60,
($item->LargeImage != null) ? $item->LargeImage->Url->getUri() :
get_bloginfo('wpurl') . $this->plugin_dir . '/img/no_image.gif',
($item->LargeImage != null) ? $item->LargeImage->Width : 60,
($item->LargeImage != null) ? $item->LargeImage->Height : 60,
$item->Label,
$item->Manufacturer,
$item->Publisher,
$item->Title,
$amazon_url,
empty($totalOffers) ? '0' : $totalOffers,
empty($lowestOfferPrice) ? '---' : $lowestOfferPrice,
$lowestOfferCurrency,
empty($amazonPrice) ? '---' : $amazonPrice,
$item->Offers->Offers[0]->CurrencyCode,
$item->Offers->Offers[0]->Availability,
get_bloginfo('wpurl') . $this->plugin_dir . '/img/amazon_' .
(empty($this->amazon_country_code) ?
'US' : $this->amazon_country_code) .'_small.gif',
get_bloginfo('wpurl') . $this->plugin_dir . '/img/amazon_' .
(empty($this->amazon_country_code) ?
'US' : $this->amazon_country_code) .'.gif',
$item->DetailPageURL,
$platform,
$author,
$artist
);
wp_cache_set($asin,$replace,'AmazonSimpleAdmin');
return preg_replace($search, $replace, $tpl);
}
wp_cache_put() がキャッシュに値を書き込む関数で、wp_cache_get() が値を読み出す関数です。
このキャッシュは、ハッシュみたいな扱いになっていて、一つのキー文字列と値をセットで格納します。AmazonSimpleAdmin をキャッシュ化するにあたっては、ASIN をキーにすることにしました。
頭の方の赤いところで、ASINをキーにしてキャッシュから値を取り出そうとします。キャッシュの中に見つかれば、その値を使って表示を作ってメソッド終了。見つからなければ、null が返ってくるので、amazon web service にクエリを投げて真面目にデータを作る、という流れです。もちろん処理の最後では、中間データをキャッシュに入れるのも忘れずに。
あ、キャッシュから値を取り出した段階(amazonにクエリを投げる前の段階)で表示を作るデータが全部確定してなきゃならないので、オリジナルのコードと比べて処理の順番を多少入れ替えてあります。
amazon商品を3個ほど貼り込んだページ を表示させてベンチマーク取ってみたところ、1ページのロード時間が 2.76sec → 0.79sec と3倍強の高速化が達成できました。素晴らしい!
とまあこんな感じで、オブジェクトキャッシュは割と簡単に使うことができます。plugin 作者の皆様におかれましては、是非ともオブジェクトキャッシュをがんがん使っていただきたい。私が喜びます(うちのサイトではXCache使ってるから)。

