避免客户端频繁向服务器提交表单的解决方案
使用redis
在order的model中增加函数
public function isDataLocked($key, $duration = 3600) { try{ $key = "lock_" . $key; $num = $this->redis->incr($key); $this->redis->expire($key, $duration); if ($num > 1) { return true; } else { return false; } } catch (Exception $e) { $this->log->logE($e->getMessage()); return false; } }
在提交表单时
if ($this->model("Model_Order")->isDataLocked($key, 5)) { return $this->err(Common_Status::OUT_OF_FRENQUENCY, "Your operation is too frequent."); }
时间: 2024-10-31 02:09:21