AccessSpeedLimit { public boolean tryAccess(String key, int seconds, int limitCount) { LimitRule limitRule = new LimitRule(); limitRule.setLimitCount(limitCount); limitRule.setSeconds(seconds); return tryAccess(key, limitRule); } AccessSpeedLimit();  AccessSpeedLimit(JedisPool jedisPool); JedisPool getJedisPool(); void setJedisPool(JedisPool jedisPool); boolean tryAccess(String key, int seconds, int limitCount); boolean tryAccess(String key, LimitRule limitRule);  }
@Test public void test1() throws InterruptedException { JedisPool jp=new JedisPool("127.0.0.1",6379); AccessSpeedLimit accessSpeedLimit=new AccessSpeedLimit(jp); SimpleDateFormat sdf=new SimpleDateFormat(" mm:ss"); while(true){ if(accessSpeedLimit.tryAccess("10.0.0.1", 1,5)){ System.out.println("yes"+sdf.format(new Date())); }else{ System.out.println("no"+sdf.format(new Date())); } Thread.sleep(100); } }
@Test public void test2() throws InterruptedException { JedisPool jp=new JedisPool("127.0.0.1",6379); LimitRule limitRule=new LimitRule(); limitRule.setSeconds(1); limitRule.setLimitCount(5); limitRule.setLockCount(7); limitRule.setLockTime(2); AccessSpeedLimit accessSpeedLimit=new AccessSpeedLimit(jp); SimpleDateFormat sdf=new SimpleDateFormat(" mm:ss"); while(true){ if(accessSpeedLimit.tryAccess("10.0.0.1",limitRule)){ System.out.println("yes"+sdf.format(new Date())); }else{ System.out.println("no"+sdf.format(new Date())); } Thread.sleep(100); } }