久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

java操作redis中如何使用expire模擬指定時間段內限制ip訪問的次數

143次閱讀
沒有評論

共計 2369 個字符,預計需要花費 6 分鐘才能閱讀完成。

行業資訊    
服務器    
云計算    
java 操作 redis 中如何使用 expire 模擬指定時間段內限制 ip 訪問的次數

java 操作 redis 中如何使用 expire 模擬指定時間段內限制 ip 訪問的次數,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面丸趣 TV 小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

    首先加入 maven 依賴,使用 JUinit 做單元測試。

dependency   
  groupId redis.clients /groupId 
 artifactId jedis /artifactId 
 version 2.7.0 /version 
  /dependency 
  
  dependency 
  groupId junit /groupId 
  artifactId junit /artifactId 
  version 3.8.1 /version 
  scope test /scope 
  /dependency

    redisutil 類,創建一個線程池,可以返回 redis 連接資源以及釋放資源

/**
 * redis 工具類,從 redis 鏈接池中獲取一個鏈接資源
 * @author Hades
 * time:2015 年 12 月 14 日
 */
public class RedisUtils {
 // 定義連接池
 public static JedisPool pool = null;
  *  獲取鏈接資源
  * @return
  */
 public static synchronized Jedis getJedis() {if(pool==null){JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
 jedisPoolConfig.setMaxTotal(100);// 最大連接數
 jedisPoolConfig.setMaxIdle(10);
 jedisPoolConfig.setMaxWaitMillis(1000);// 類似于超時時間
 jedisPoolConfig.setTestOnBorrow(true);
 pool = new JedisPool(jedisPoolConfig, 192.168.57.133 ,6379);// 創建連接池
 Jedis jedis = pool.getResource();
 return jedis;
  *  釋放鏈接資源
  * @param jedis
  */
 public static void returnJedis(Jedis jedis) {pool.returnResourceObject(jedis);
 }

    redis 客戶端類,使用的是 juinit 做單元測試哈

/**
 * redis 測試類
 * @author Hades
 *
 */
public class RedisTest {static Jedis jedis =RedisUtils.getJedis();
 @Test
 public void test3() throws Exception {
 String ip = 192.168.57.2 // 訪問的 ip
 // 測試
 for (int i = 0; i   20; i++) {boolean flag = testLogin(ip);
 System.out.println(flag);
  *  模擬限制 ip 指定時間段內訪問次數
  * @param ip
  * @return
  */
 public boolean testLogin(String ip) {String value = jedis.get(ip);
 if(value==null){
 jedis.set(ip,  1 
 jedis.expire(ip, 60);// 設置過期時間 60 秒
 return true;
 }else{int parseInt = Integer.parseInt(value);
 //60 秒內訪問超過 10 次,就禁止訪問
 if(parseInt 10){
 System.out.println( 訪問受限!!!!return false;
 jedis.incr(ip);
 return true;
 } 
  *  不使用管道   向 jedis 插入一萬條數據消耗時間:3184
  */
 @Test
 public void test2() throws Exception{
 // TODO Auto-generated method stub
 long start = System.currentTimeMillis();
 for (int i = 0; i   10000; i++) {
 jedis.set( a +i, i+ 
 jedis.expire(a +i, 60);
 System.out.println(System.currentTimeMillis()-start);
  *  使用管道命令批量導入數據   所需時間:204
  * @throws Exception
  */
 @Test
 public void test4() throws Exception {long start = System.currentTimeMillis();
 Pipeline pipelined = jedis.pipelined();
 for (int i = 0; i   10000; i++) {
 pipelined.set( a +i, i+ 
 pipelined.expire(a +i, 60);
 pipelined.sync();
 System.out.println(System.currentTimeMillis()-start);
}

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注丸趣 TV 行業資訊頻道,感謝您對丸趣 TV 的支持。

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-25發表,共計2369字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 牙克石市| 文水县| 临夏市| 永丰县| 赣榆县| 郎溪县| 繁昌县| 青阳县| 瓦房店市| 晴隆县| 新津县| 格尔木市| 邢台市| 海淀区| 金阳县| 南乐县| 宝丰县| 武城县| 涞水县| 岳阳县| 隆尧县| 和平县| 若尔盖县| 静乐县| 湖州市| 荥阳市| 张家界市| 日土县| 双辽市| 潍坊市| 炉霍县| 新晃| 砚山县| 长宁县| 贵定县| 随州市| 彭泽县| 卢龙县| 荔波县| 蒙阴县| 静海县|