php8.5
Home/ Manual/ memcached / memcached/ Memcached::set

Memcached::set

PHP function Edit on GitHub ✎

(PECL memcached >= 0.1.0)

Store an item

Description

Memcached::set(string $key, mixed $value, int $expiration = 0): bool

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.

Parameters

key

Key

value

Value

expiration

Expiration

Return Values

Success Getresultcode

Examples

Memcached::set() example

php
<?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'));
?>

The above example will output something similar to:

output
int(99)
string(15) "a simple string"
array(2) {
  [0]=>
  int(11)
  [1]=>
  int(12)
}
object(stdClass)#1 (0) {
}

See Also

  • Memcached::setByKey
  • Memcached::add
  • Memcached::replace

Source: reference/memcached/memcached/set.xml · from the official PHP manual (php/doc-en)