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

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

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

}

 

posted @ 2016-07-01 10:57  慕尘  阅读(1107)  评论(0编辑  收藏  举报