'beforeAction']; } /** * @param $event * @throws TooManyRequestsHttpException */ public function beforeAction($event) { // 只有post方式才可以需要此处理 if ($this->userId && Yii::$app->request->isPost) { /** @var Connection $redis */ $redis = Yii::$app->redis; // 限流: 用户 + 访问方法 $key = $this->prefix.sprintf('hist_%s_%s', $this->userId, Yii::$app->controller->route); $res = $redis->set($key,1,'NX','EX',$this->second); if (!$res) throw new TooManyRequestsHttpException('服务器繁忙,路径:'.Yii::$app->controller->route); } } }