zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

使用redis避免客户端频繁提交数据

Redis客户端数据 提交 避免 频繁 使用
2023-09-14 09:00:01 时间

避免客户端频繁向服务器提交表单的解决方案

使用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.");

}