(PECL memcache >= 0.2.0)
Memcache::set — Store data at the server
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.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.
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).
Example #1 Memcache::set() example
<?php
$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_set($memcache_obj, 'var_key', 'some variable', 0, 30);
echo memcache_get($memcache_obj, 'var_key');
?>
Example #2 Memcache::set() example
<?php
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$memcache_obj->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);
echo $memcache_obj->get('var_key');
?>