CakeFest 2024: The Official CakePHP Conference

Callbacks de cache de leitura

Os callbacks do cache de leitura são invocados quando um item não pode ser recuperado do servidor. O callback é passado para o objeto Memcached, a key solicitada e a variável de valor por referência. O callback é responsável por definir o valor e retornar true ou false. Se o callback retornar true, o Memcached armazenará o valor preenchido no servidor e o retornará à função de chamada original. Somente Memcached::get() e Memcached::getByKey() suportam esses callbacks, porque o protocolo memcache não fornece informações sobre quais keys não foram encontradas na solicitação de várias keys.

Exemplo #1 Exemplo de callback de leitura

<?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);
/* lookup profile info in the 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