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