The method correctly returns false if you set the value to false. This means that in order to have proper fault checking mechanism in place you need to check the result code.
<?php
$Memcached = new Memcached();
$Memcached->addServer('localhost', 11211);
$Memcached->set('key', false);
var_dump($Memcached->get('key')); // boolean false
var_dump($Memcached->getResultCode()); // int 0 which is Memcached::RES_SUCCESS
?>
Memcached::set
(PECL memcached >= 0.1.0)
Memcached::set — Store an item
Descrição
Memcached::set() stores the value on a memcache server under the specified key . The expiration parameter can be used to control when the value is considered expired.
The value can be any valid PHP type except for resources, because those cannot be represented in a serialized form. If the Memcached::OPT_COMPRESSION option is turned on, the serialized value will also be compressed before storage.
Parâmetros
- key
-
A chave sobre a qual guardar o valor.
- value
-
O valor para guardar.
- expiration
-
O tempo de expiração, padrão é 0. Veja Tempos de Expiração para maiores informações.
Valor Retornado
Retorna TRUE em caso de sucesso ou FALSE em falhas. Use Memcached::getResultCode se necessário.
Exemplos
Exemplo #1 Memcached::set() example
<?php
$m = new Memcached();
$m->addServer('localhost', 11211);
$m->set('int', 99);
$m->set('string', 'a simple string');
$m->set('array', array(11, 12));
/* expire 'object' key in 5 minutes */
$m->set('object', new stdclass, time() + 300);
var_dump($m->get('int'));
var_dump($m->get('string'));
var_dump($m->get('array'));
var_dump($m->get('object'));
?>
O exemplo acima irá imprimir algo similar a:
int(99)
string(15) "a simple string"
array(2) {
[0]=>
int(11)
[1]=>
int(12)
}
object(stdClass)#1 (0) {
}
Veja Também
- Memcached::setByKey - Store an item on a specific server
- Memcached::add - Add an item under a new key
- Memcached::replace - Replace the item under an existing key
Memcached::set
16-Jul-2009 01:26
