Examples of preventing duplicate submissions through Redis

Source: Internet
Author: User
Tags commit redis

The use of Redis can be very convenient to prevent the form or button repeatedly submitted to the action lock, in a certain period of time can not be executed.

Prevent duplicate Submissions
$lock _nopay = ' Nopay_ '. $SB _id. $SB _uid. $pay _money;
if (! $this->lock ($lock _nopay)) {
$this->error ("The current cancellation operation is not completed, do not repeat!") ");
}

This calls the lock method, locks for 10 seconds, and interrupts the operation within 10 seconds if it is resubmitted again, and is considered a duplicate commit.


The lock method is written like this:


/**
* Lock Request
* @param $expire lockout time
* @param $mark Custom Tags
* @return BOOL
*/
protected function Lock ($expire =1, $mark = ') {
$url =strtolower (module_name. ') /'. Action_name);//Operation address
$prefix = ' Lock_ '; $sid =session_id ();
$key = $prefix. MD5 ($sid. $url. $mark);//Token key value
if (D (' Redis ')->incr ($key) ==1) {
Return D (' Redis ')->expire ($key, $expire);
}
return false;
}
Use the Redis incr method, and the expire caching time method, to determine whether the same request is in a certain amount of time, and if the same request returns false, preventing the form or button from repeating the submission.

When the method completes, you need to unlock the duplicate commit.


Unlock
$this->unlock ($lock _nopay);

This is the complete method of using Redis to prevent duplicate submissions, or button repeat clicks

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.