PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Memcache::setCompressThreshold> <Memcache::replace
Last updated: Fri, 15 Aug 2008

view this page in

Memcache::set

(PECL memcache:0.2-2.1.2)

Memcache::setStore data at the server

Description

bool Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] )

Memcache::set() stores an item var with key on the memcached server. Parameter expire is expiration time in seconds. If it's 0, the item never expires (but memcached server doesn't guarantee this item to be stored all the time, it could be deleted from the cache to make place for other items). You can use MEMCACHE_COMPRESSED constant as flag value if you want to use on-the-fly compression (uses zlib).

Note: Remember that resource variables (i.e. file and connection descriptors) cannot be stored in the cache, because they cannot be adequately represented in serialized state.

Also you can use memcache_set() function.

Parameters

key

The key that will be associated with the item.

var

The variable to store. Strings and integers are stored as is, other types are stored serialized.

flag

Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).

expire

Expiration time of the item. If it's equal to zero, the item will never expire. You can also use Unix timestamp or a number of seconds starting from current time, but in the latter case the number of seconds may not exceed 2592000 (30 days).

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example #1 Memcache::set() example

<?php
/* procedural API */

/* connect to memcached server */
$memcache_obj memcache_connect('memcache_host'11211);

/*
set value of item with key 'var_key'
using 0 as flag value, compression is not used
expire time is 30 seconds
*/
memcache_set($memcache_obj'var_key''some variable'030);

echo 
memcache_get($memcache_obj'var_key');

?>

Example #2 Memcache::set() example

<?php
/* OO API */

$memcache_obj = new Memcache;

/* connect to memcached server */
$memcache_obj->connect('memcache_host'11211);

/*
set value of item with key 'var_key', using on-the-fly compression
expire time is 50 seconds
*/
$memcache_obj->set('var_key''some really big variable'MEMCACHE_COMPRESSED50);

echo 
$memcache_obj->get('var_key');

?>



Memcache::setCompressThreshold> <Memcache::replace
Last updated: Fri, 15 Aug 2008
 
add a note add a note User Contributed Notes
Memcache::set
bluej100 at gmail
08-Aug-2008 03:25
To expand on eseaberg's note somewhat, the connection is marked as failed on a *get* for an unserializable value on a key marked with the undocumented serialized flag. The initial assignment returns true. This is probably expected behavior, but I was confused when I forgot the flags argument and passed in 5 seconds for an expiration time.

<?php
$server
= 'wwwdev.daz3d.com';

$m = new Memcache;
var_dump($m->connect($server));
var_dump($m->flush());
var_dump($m->set('foo', serialize('bar'), 1));
var_dump($m->get('foo'));
var_dump($m->set('foo', 'bar', 1));
var_dump($m->set('foo', 'baz'));
var_dump($m->get('foo'));        // 'baz'
var_dump($m->set('foo', 'bar', 1));
var_dump($m->get('foo'));        // false
var_dump($m->set('foo', 'bar'));
var_dump($m->connect($server));
var_dump($m->set('foo', 'bar'));
var_dump($m->get('foo'));
?>
argyleblanket
25-Jun-2008 08:12
Using set more than once for the same key seems to have unexpected results - it does not behave as a "replace," but instead seems to "set" more than one value for the same key.  "get" may return any of the values.

This was tested on a multiple-server setup - behaviour may be different if you only have one server.

Remedy is to use a combination of replace and set:

$result = $memcache->replace( $key, $var );
if( $result == false )
{
    $result = $memcache->set( $key, $var );
}
eseaberg at adbrite dot com
11-Jun-2008 06:00
"Other types are stored serialized" isn't true, at least as of PHP 5.2.0 with memcache extension $Revision: 1.85 $.

Null is serialized, but for an int, float, or bool, Memcache::set converts the value to a string (actually it tampers with the caller's zval, as if taking it by reference and using settype) and Memcache::get will return a string instead of the original type.

Using $memcache->set($key, serialize($var), 1) is a workaround. This undocumented bit in $flag tells Memcache::get to deserialize the value it reads. If this bit is set on a value that can't be deserialized, the server is marked as failed.
phalcos
25-Sep-2007 02:46
Here is a very visual example on how to use the memcache. It sets the key 'time' to the current time.
Put the example code in an empty page and keep reloading it.
As you can see, after 10 seconds your memcache key will be able to update with a new timestamp.

<?php

$memcache_obj
= new Memcache;
$memcache_obj->connect('127.0.0.1', 11211);

if (
$memcache_obj->get('time') == "") {
       
$date = date("H:i:s");
       
$memcache_obj->set('time', $date, MEMCACHE_COMPRESSED, 10);
}

echo
"At ".date("H:i:s").", your key is ".$memcache_obj->get('time');

?>

Memcache::setCompressThreshold> <Memcache::replace
Last updated: Fri, 15 Aug 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites