Memcache::set
memcache_set
Store data at the server
Description
bool Memcache::set(
string $key
,
mixed $var
,
int $flag
= ?,
int $expire
= ?
)
bool memcache_set(
Memcache $memcache
,
string $key
,
mixed $var
,
int $flag
= ?,
int $expire
= ?
)
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.
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', 0, 30);
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_COMPRESSED, 50);
echo $memcache_obj->get('var_key');
?>
See Also
- Memcache::add
- Memcache::replace