Increment usage of memcache

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.