Memcache::set

(no version information, might be only in CVS)

Memcache::set -- Store 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).

Also you can use memcache_set() function. See example below.

注: 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.

例子 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', 0, 30);

echo
memcache_get($memcache_obj, 'var_key');

?>

例子 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_COMPRESSED, 50);

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

?>

如果成功则返回 TRUE,失败则返回 FALSE

See also Memcache::add(), Memcache::replace().


add a note add a note User Contributed Notes
mars at joyinter dot com
02-Aug-2006 10:06
Something's really weird between Memcached API between PHP and java(com.danga.MemCached)

first, when PHPAPI set a pair of key and value,java can't read it:

PHP code:
<?php
/* OO API */

$memcache_obj = new Memcache;

/* connect to memcached server */
$memcache_obj->connect('192.168.0.200', 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');

?>

Java code:

try {
     SockIOPool pool = SockIOPool.getInstance();
     pool.setServers(new String[]{"192.168.0.200:11211"});
     pool.initialize();

     client = new MemCachedClient();
     System.out.println("value="+client.get("var_key"));
   }
   catch (Exception e) {
     e.printStackTrace();
   }

The result turns out to be : value=null

but when use javacode:
System.out.println(client.replace("var_key", "xxx"));
System.out.println("value="+client.get("var_key"));
The result:
true
xxx

It indicates java actually read the key!?

On the other hand, when Java set a pair of key and value, php read it, the value will have an extra char at the front of actual value:

Java Code:
try {
     SockIOPool pool = SockIOPool.getInstance();
     pool.setServers(new String[]{"192.168.0.200:11211"});
     pool.initialize();

     client = new MemCachedClient();
     client.set("somekey", "somevalue");
   }
   catch (Exception e) {
     e.printStackTrace();
   }

PHP Code:
<?php
/* OO API */

$memcache_obj = new Memcache;

$memcache_obj->connect('192.168.0.200', 11211);

$value = $memcache_obj->get('somekey');
echo
"value=$value\r\nFirst char's ASCII value is ".ord($value[0]);

?>

value=somevalue
First char's ASCII value is 6

Is anybody able to explain that?