使用jedis2.8.0串連redis

來源:互聯網
上載者:User

標籤:

   下載了最新的jedis用戶端jedis2.8.0,在網上找了找jedis使用串連池的工具類,拿來發現都是低版本的jedis寫法:

    returnResource();

    returnBrokenResource();

    這倆方法到期了~,查不到最新版本的寫法,裝逼未遂~~  

    於是去看了看源碼,發現源碼的注釋寫的很清楚:

/**   * @deprecated starting from Jedis 3.0 this method will not be exposed.   * Resource cleanup should be done using @see {@link redis.clients.jedis.Jedis#close()}   */  @Override  @Deprecated  public void returnResource(final Jedis resource) {    if (resource != null) {      try {        resource.resetState();        returnResourceObject(resource);      } catch (Exception e) {        returnBrokenResource(resource);        throw new JedisException("Could not return the resource to the pool", e);      }    }  }

用close()方法就可以了,跟蹤看看close()方法:

@Override  public void close() {    if (dataSource != null) {      if (client.isBroken()) {        this.dataSource.returnBrokenResource(this);      } else {        this.dataSource.returnResource(this);      }    } else {      client.close();    }  }

dataSource 為定義的串連池 ,如果串連池不為空白,做一些歸還串連的操作,jedis的串連池設計都是基於org.apache.commons.pool2,這倆方法沒跟著看,不過就是將串連的屬性值改為了可用之類的。

如果串連池為空白的話,直接斷開與redis服務的串連啦~

 

所以在寫一些基礎類的時候,看起來應該是這樣的:

public <T> void set(T item) {        Jedis jedis =null;        try {            jedis = JedisClient.getJedis();            String v = cacheKeyUtil.getDefaultKey(item);            jedis.set(v, iserializer.serialize(item));        } catch (Exception e) {            logger.warn("set", item, e);        } finally {
      if(null != jsdis){
    jedis.close();
        }                    }    }

         借了別人的,一定要還~~~

 

使用jedis2.8.0串連redis

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.