thinkphp版

//避免频繁请求 (ThinkPHP)
 $sessID = 'register' . $data['mobile_code_id'] . $data['mobile'] . $data['password'];
 if(!(new \think\session\driver\Redis())->lock($sessID,3)){
     throw new \Exception('频繁请求');
 }

通用写法

$lock_key = 'LOCK_PREFIX' . $redis_key;
$is_lock = $redis->setnx($lock_key, 1); // 加锁
if($is_lock == true){ // 获取锁权限
     // 设置过期时间,防止死任务的出现
     $redis->expire($lock_key, 5);
} else {
     return true; // 获取不到锁权限,直接返回
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-05
  • 2021-08-05
  • 2019-01-05
  • 2021-11-05
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-07
  • 2022-12-23
相关资源
相似解决方案