PHP uses redis prevent large secondary concurrent write (redis lock)

// to avoid frequent requests (ThinkPHP) 
        $ sessid = 'Register'. $ Data [ 'mobile_code_id']. $ Data [ 'Mobile']. $ Data [ 'password' ];
         IF ! (( New new \ Think \ the session \ Driver \ the Redis ()) -> Lock ( $ sessid ,. 3 )) {
             the throw  new new \ Exception ( 'frequently requested' );
         }
// define their own 
$ lock_key = 'LOCK_PREFIX'. $ Redis_key ;
         $ is_lock = $ Redis -> SETNX ( $ lock_key ,. 1); // lock 
        IF ( $ is_lock == to true ) { // Get lock permissions 
            // Set expiration time, to prevent the death of tasks 
            $ Redis -> the expire ( $ lock_key , 5 ); 
        } the else {
             return  to true ; // not obtain permission to lock directly back 
        }

 

Guess you like

Origin www.cnblogs.com/weihua2018/p/11359677.html