思路

用户请求限限流(同接口访问时间限制)

> redis key 接品名+ user_id 每访问一次+1 过期时间为限制访问时间


_user_id         = (int)$params['user_id'];
    $this->_search_key      = $params['apiName'].$this->_prefix;
    $this->_key             = $this->_search_key.$params['user_id'];
    $this->_expireat_time   = 60;##配置项
    $this->_user_max_limit  = 100;##配置项
    $this->_max_limit       = 100;##配置项

   } 
   /**
    * [userLimit 是否达到单用户接口请求峰值]
    * @Author   Jerry                    (wx621201)
    * @DateTime 2019-08-02T15:35:52+0800
    * @Example  eg:
    * @return   [type]                   [description]
    */
   public function checkUserLimit(){
      if($this->_user_max_limit==0) return true;//0不限流,不存在防刷,可以购买
      ##如果不存在,说明超过一分钟,给SQL拦截
      $matchNum = $this->_exists();
      $matchNum = (int)$matchNum?$matchNum:0;
      if($matchNum>$this->_user_max_limit) return false;##达到限流条件
      $this->_set($matchNum+1);
      return true;##默认通过
   }
   /**
    * [countLimit 是否达到用户限流数]
    * @Author   Jerry                    (c84133883)
    * @DateTime 2019-08-05T10:26:39+0800
    * @Example  eg:
    * @return   [type]                   [description]
    */
   public function checkCountLimit(){
      if($this->_max_limit==0) return true;//0不限流
      if($this->_userCount>$this->_max_limit){
        return false;
      }else{
        return true;
      }
   }
   /**
    * [_userCount 总用户数]
    * @Author   Jerry                    (wx621201)
    * @DateTime 2019-08-02T15:41:08+0800
    * @Example  eg:
    * @return   [type]                   [description]
    */
   private function _userCount(){
        $list = redis::scene($this->_scene)->keys($this->_search_key.'*');
        return count($list);
   }
   /**
    * [_set description]
    * @Author   Jerry                    (wx621201)
    * @DateTime 2019-08-02T15:45:51+0800
    * @Example  eg:
    * @param    [type]                   $key       [description]
    * @param    [type]                   $value     [description]
    * @param    integer                  $time      [description]
    * @param    string                   $scene     [description]
    */
   private function  _set($value){
        redis::scene($this->_scene)->set($this->_key,$value);
         ##处理过期时间
        $expiretime = time()+(float)$this->_expireat_time;##1分钟
        redis::scene($this->_scene)->expireat($this->_key,$expiretime);
        return ture;
    }
  /**
   * [_exists 检查是否存在]
   * @Author   Jerry                    (wx621201)
   * @DateTime 2019-08-02T15:45:54+0800
   * @Example  eg:
   * @param    [type]                   $key       [description]
   * @param    string                   $scene     [description]
   * @return   [type]                              [description]
   */
   private function _exists(){
        if(!redis::scene($this->_scene)->exists($this->_key)||!redis::scene($this->_scene)->get($this->_key)){
            return false;
        }
        return redis::scene($this->_scene)->get($this->_key);
    }

 
  

   
}

总的用户限流

求合上面的key数,得出当前所在的用户数量

所有请求限流

思路1 通过IP限流