PHP in the operation of memcache data, we are accustomed to using get,set,delete more, suddenly found that increment and decrement is actually very useful, heard that the use of appropriate program performance is very helpful.
First look at the instructions in the document:
Memcache::increment () adds value to the specified element. This value is modified to value if the specified key corresponds to an element that is not a numeric type and cannot be converted to a numeric value.
Memcache::d ecrement () method to reduce the value of the element. Similar to the Memcache::increment () method, first converts the current value of the element to a numeric value and then subtracts value.
In fact, its role is to quickly memcache stored in the data to increase or reduce a certain value, but the premise is that the data stored in the memcache is a numerical type, which requires the design of the cache database when the use of the number of often used to separate out.
Instance:
We used to do this to add 1 to the value of key ' a '.
<?php
$memcache _obj = new Memcache;
$memcache _obj->connect (' localhost ', 11211) or die ("could not Connect");
if ($s = $memcache _obj->get (' a ')) {
$s = $s +1;
$memcache _obj->set (' a ', $s);
}
Else
$memcache _obj->set (' A ', 1);
Echo $s;
?>
Now we can use the increment method to manipulate
<?php
$memcache _obj = new Memcache;
$memcache _obj->connect (' localhost ', 11211) or die ("could not Connect");
if ($s = $memcache _obj->increment (' A ', 1)} {
echo $s;
}
Else
$ Memcache_obj->set (' A ', 1);
?> reproduced in: http://garey.bsdart.org/2011/03/%E5%85%B3%E4%BA%8Ememcacheincrement%E5%92%8Cmemcachedecrement%E7% 9a%84%e4%bd%bf%e7%94%a8/