关键代码如下:

@Override 

 public void rateLimit(String key, int unitTime, long rate) { 

 Jedis jedis = getJedis(); 

 Long i = jedis.setnx(key, "1"); 

 if (i == 1L) {// 第一次设置过期时间 

 jedis.expire(key, unitTime); 

 } 

 Long incr = jedis.incr(key); 

 returnRes(jedis); 

 if (incr > rate) { 

 throw new RuntimeException(key + "在[" + unitTime + "]s  内访问超过[" + rate + "]次"); 

 } 

 }