Php/zend framework/zend cache/memcached
提供: 初心者エンジニアの簡易メモ
データキャッシュ
// FrontEnd に関する設定
$frontendOptions = array(
'lifetime' => 7200, // キャッシュの有効期限を 2 時間とする
'automatic_serialization' => true
);
// BackEndに関する設定
$backendOptions = array(
'servers' => array(
array(
'host' => 'localhost',
'port' => 11211,
'persistent' => true
)
)
);
// キャッシュオブジェクトを生成
$cache = Zend_Cache::factory('Output', 'Memcached', $frontendOptions, $backendOptions);
$cache_id = md5($id);
// キャッシュがすでに存在するかどうかを調べます
if (($result = $cache->load($cache_id)) === false) {
// キャッシュが見つかりませんでした。
// 処理
$result = $logic->exec();
$cache->save($result, $cache_id);
} else {
// キャッシュが見つかりました!
}
return $result;
Zend_CacheでConsistentHashingを使う
ver1.11で追加されたようです
// BackEndに関する設定
$backendOptions = array(
'servers' => array(
array(
'host' => '192.168.0.1',
'port' => 11211,
'weight' => 50,
),
array(
'host' => '192.168.0.2',
'port' => 11211,
'weight' => 50,
)
)
);
// キャッシュオブジェクトを生成
$cache = Zend_Cache::factory('Output', 'Libmemcached', $frontendOptions, $backendOptions);
