Principle and example of redis locking to prevent repetitive operation

Source: Internet
Author: User
Tags md5 numeric value redis

Using Redis locking method can prevent repetitive operations, including form submission, Ajax and other HTTP requests, it is the principle of Redis incr command to the key from the increase, if the return value is not 1, it is a duplicate request, specific look at the code:

The code is as follows Copy Code

/**
* Lock Request
* @param int $expire lockout time
* @param string $mark Custom tags
* @return BOOL
*/
protected function Lock ($expire =1, $mark = ') {
$url =strtolower (module_name. ') /'. Action_name);//Operation address
$prefix = ' Lock_ '; $sid =$_cookie[c ("Session_options.name")];
$key = $prefix. MD5 ($sid. $url. $mark);//Token key value
/**
* @var Redis $redis
*/
$redis = D (' Redis ');
if ($redis->incr ($key) ==1) {
Return $redis->expire ($key, $expire);
}
return false;
}

Here also talk about the use of the Redis incr command:

INCR Key Command usage

Increases the number of values stored in the key by one.

If the key does not exist, then the value of the key is initialized to 0 before performing the INCR operation.

If the value contains the wrong type, or if the value of the string type cannot be represented as a number, an error is returned.

The value of this operation is limited to a 64-bit (bit) signed digit representation.

The code is as follows Copy Code

redis> SET Page_view 20
Ok

Redis> INCR Page_view
(integer) 21

Redis> get Page_view # numeric value is saved as a string in Redis
"21"

The return value is the value after the INCR.

If incr a nonexistent key, it automatically creates the key and returns a value of 1.

Expire Command Example:


Expire Key 22

Set the expiration time of the key, which must exist, to be 22s.

Go back to the lock method defined at the beginning, the key value is lock+ the current operation address +cookie value + custom string, perform incr operation and set expiration default 1s with the expire command, if the first request returns 1 (Expire set cache succeeded), Indicates that the set cache time succeeds, otherwise false indicates a duplicate request or operation.

When in use:

The code is as follows Copy Code
if (! $this->lock (1)) {
Repeat operation.
}

This can be judged to be repeated operation.

The code is as follows Copy Code


$this->unlock ()

This unlocks, allowing the same commit for the second time.

Look at the Unlock method:

  code is as follows copy code

 

/**
 * unlock request
 * @param string $mark Custom token
 * @return bool
 */
protected function un Lock ($mark = ') {
    $url =strtolower module_name. ' /'. Action_name)//Operation address
    $prefix = ' Lock_ '; $sid = $sid =$_cookie[c ("Session_options.name")];
    $key = $prefix. MD5 ($sid. $url. $mark);//token key value
   /**
      * @var Redis $redis
     */
    $redis = D (' Redis ');
  &N Bsp Return $redis->del ($key);
}

The Redis del command is used here.

Related Article

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.