CakeFest 2024: The Official CakePHP Conference

Read-through キャッシュコールバック

Read-through キャッシュコールバックが起動されるのは、 アイテムをサーバーから取得できなかったときです。 このコールバックに渡されるのは Memcached オブジェクト、 要求されたキー、そして値を格納する変数への参照です。 このコールバックの役割は、値を設定して true あるいは false を返すことです。 コールバックが true を返した場合は、 Memcached は設定された値をサーバーに格納して呼び出し元の関数にその値を返します。 このコールバックをサポートしているのは Memcached::get()Memcached::getByKey() のみです。 というのも memcache プロトコルでは、 複数のキーが要求されたときにどのキーが見つからなかったのかを知る方法がないからです。

例1 Read-through コールバックの例

<?php
$m
= new Memcached();
$m->addServer('localhost', 11211);

$profile_info = $m->get('user:'.$user_id, 'user_info_cb');

function
user_info_cb($memc, $key, &$value)
{
$user_id = substr($key, 5);
/* プロファイル情報を DB から探します */
/* ... */
$value = $profile_info;
return
true;
}
?>
add a note

User Contributed Notes 2 notes

up
2
chadkouse
12 years ago
Or just set the value within the callback with your own custom expiration time and return false. I think it's cleaner.
up
1
oorza2k5 at gmail dot com
15 years ago
This isn't specified anywhere, so I had a gander at the source...

The expiry on read-through cache set values is set to 0, or forever. This means if you want your key to implicitly expire, don't use the callback methods, instead check for boolean false as a return and manually set the value, at least for now.
To Top